我正在尝试读取如下所示的 JSON 文件。它们是推文的时间戳。当我用我的代码读入文件时,它作为一个大字符串出现。有没有办法让他们分开。当我使用 str.split() 时,它会分割所有内容。有没有一个可以让我加载或取出来使这个更容易
"Sat Aug 06 23:54:24 +0000 2016""Sat Aug 06 23:54:24 +0000 2016""Sat Aug 06 23:54:24 +0000 2016""Sat Aug 06 23:54:24 +0000 2016"
这是我的阅读方式
q = 'Trump'
twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
stream = twitter_stream.statuses.filter(track=q)
for tweet in stream:
print (type(tweet))
tweet = tweet['created_at']
with open('dates.json', 'a') as outfile:
json.dump(tweet, outfile, indent=4)
这是我目前正在尝试的方法
with open('dates.json', 'rb') as f:
data = f.readlines()
我希望它们按日期分隔,这样我就可以将它们隐藏起来以制作时间序列图
编辑/更新:现在我有了这个,但是流只是不断地收集推文而不停止。如何让它停止收集推文并将 JSON 数据转储到文件中。无论是手动还是自动
q = 'Trump'
twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
stream = twitter_stream.statuses.filter(track=q)
dates = [tweet['created_at'] for tweet in stream]
with open('dates.json', 'a') as outfile:
json.dump(dates, outfile, indent=4)
收集推文日期到列表中,然后转储一次:
dates = [tweet['created_at'] for tweet in stream]
with open('dates.json', 'a') as outfile:
json.dump(dates, outfile, indent=4)
<小时/>
With this, how do I get it to stop streaming and dump into the file. Before since it was dumping tweet by tweet I would just restart the shell.
我认为你应该将理解扩展到常规循环并将其放入 try/finally
中:
dates = []
try:
for tweet in stream:
dates.append(tweet['created_at'])
finally:
with open('dates.json', 'a') as outfile:
json.dump(dates, outfile, indent=4)
我是一名优秀的程序员,十分优秀!