我有一个包含报纸文章的json文件。每行包含文章的日期、标题和正文。我想创建一个特定关键字出现在文本中的月份计数。到目前为止,我只能打印整个日期,但我希望检索到月份计数:例如,而不是一月,一月,一月;将产生计数的内容:一月=3或类似的内容。到目前为止,我的代码如下:
# import json module for parsing
import json
import re
# define a list of keywords
keywords = ('tax', 'Tax', 'policy', 'Policy', 'regulation', 'Regulation',
'spending', 'Spending', 'budget', 'Budget', 'oil', 'Oil',
'Holyrood', 'holyrood', 'Scottish parliament', 'Scottish Parliament', 'scottish parliament' )
with open('Aberdeen2005.json') as json_file:
# read json file line by line
for line in json_file.readlines():
json_dict = json.loads(line)
if any(keyword in json_dict["body"].lower() for keyword in keywords):
print(json_dict['date'].split()[0])
这里只是一个示例,因为您没有提供JSON文件的样子
输出
你可以用熊猫试试这个:
相关问题 更多 >
编程相关推荐