python - 如何将 JSON 文件作为列表中的单独字符串而不是作为一个大列表读取

标签 python json twitter

我正在尝试读取如下所示的 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)

关于python - 如何将 JSON 文件作为列表中的单独字符串而不是作为一个大列表读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38809744/

相关文章:

python - wrapper() 得到了一个意外的关键字参数 'id'

python - 自动装饰类中的每个实例方法

javascript - 如何动态地创建/输入一个项目作为无序列表中另一个项目的子列表项目?

ios - 在 iOS 11 中分享到 Twitter,无需侵入性请求

python - 在 tweepy 中回复推文

python - bool 值总是返回 true? ( Pandas )

python - 如何从客户端接收 int 和 string 的缓冲区,并正确存储它们? (cpp 服务器,python 客户端)

java - Jackson 将 Object 序列化为 JSON 到 base64(没有无限循环)

javascript - Google API 路径未显示

php - 了解数据库表的 JSON 输出