代码之家  ›  专栏  ›  技术社区  ›  Teslaturing

在python中从结果中获取指定单词后的动态数字并存储在数据库中

  •  0
  • Teslaturing  · 技术社区  · 7 年前

    嗨,我想得到结果中“citedby”后面的数字。每次搜索的数字都会发生变化`

    import scholarly
    import re
    
    m = next(scholarly.search_pubs_query('Perception of physical stability and center of mass of 3D objects'))
    n = re.search('citedby (\d+)', m , re.IGNORECASE)`
    

    我用学术方法查找引文,并存储在m变量中。现在我想得到“citedby”后面的数字:34567。示例现在,我想在“citedby”之后获取34567:。请帮帮我,我是python新手。添加了示例结果。 Result ,则, error

    1 回复  |  直到 7 年前
        1
  •  1
  •   Austin    7 年前

    您可以尝试使用 findall 它以字符串列表的形式返回Patterninstring的所有非重叠匹配项。

    import re
    m = "Example text 'citedby':34567"  # just an example.
    n = re.findall(r"'citedby':\s?(\d+)", m, re.IGNORECASE)
    print(' '.join(n))  # 34567
    

    对于您的具体问题:

    import scholarly
    import re
    
    m = next(scholarly.search_pubs_query('Perception of physical stability and center of mass of 3D objects'))
    n = re.findall(r"'citedby':\s?(\d+)", str(m), re.IGNORECASE)
    print(''.join(n))  # 13
    

    笔记 :此处 m <class 'scholarly.Publication'> 对象 str(m) 成功了 <class 'str'> 芬德尔 仅适用于字符串。