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

2024-10-06 11:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我想得到结果中“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)`

我用scholarly查找引文并存储在m变量中。现在我想获取“citedby”后面的数字:34567。示例现在我想获取“citedby”后面的34567:。请帮助我,我是python新手。添加了示例结果。Resulterror


Tags: andofimportre示例search数字query
1条回答
网友
1楼 · 发布于 2024-10-06 11:25:33

您可以尝试使用^{}作为字符串列表,它返回字符串中模式的所有非重叠匹配项

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'>findall仅适用于字符串

相关问题 更多 >