在最近的尤文图斯和马德里竞技之间的冠军联赛比赛之前,我收集了一个小时的直播推文。
#setting tweepy up
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
translator = Translator()
#gathering live tweets with probable hashtags for the fixture for an hour before the game starts
class Listener(StreamListener):
def on_data(self, status):
print(status)
with open('Juve_vs_AthMadrid.json', 'a') as f:
f.write(status)
return True
def on_error(self, status):
print(status)
return True
twitter_stream = Stream(auth, Listener())
twitter_stream.filter(track=['#Juve', '#juve', '#JuveAtleti', '#turin',
'#AúpaAtleti', '#ForzaJuve', '#AtléticosAroundTheWorld!', '#VamosAtleti',
'#AtléticosPorElMundo'])
接下来,我继续清理数据。我在其中创建了带有每个tweet字典(作为字符串)的列表,并尝试使用 json.loads函数
将这些字符串转换为实际的python字典。handle = open('Juve_vs_AthMadrid.json', 'r')
file = handle.readlines()
handle.close()
dic_list = []
for dic_str in file:
dic_list.append(json.loads(dic_str))
但是,我不断从dic_list.append(json.loads(dic_str))
行的None中得到 raise JSONDecodeError(“ Expecting value”,s,err.value)错误
答案 0 :(得分:1)
读取JSON文件并将数据存储在Python字典中的示例:
example.py
:
import json
with open("example.json", "r") as json_data:
data = json.loads(json_data.read())
print(type(data))
print(data)
example.json
:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
输出:
<class 'dict'>
{'glossary': {'GlossDiv': {'GlossList': {'GlossEntry': {'SortAs': 'SGML', 'Abbrev': 'ISO 8879:1986', 'ID': 'SGML', 'GlossTerm': 'Standard Generalized Markup Language', 'GlossDef': {'GlossSeeAlso': ['GML', 'XML'], 'para': 'A meta-markup language, used to create markup languages such as DocBook.'}, 'GlossSee': 'markup', 'Acronym': 'SGML'}}, 'title': 'S'}, 'title': 'example glossary'}}
输出显示data
是dict
类型。