【发布时间】:2017-12-24 05:38:03
【问题描述】:
我有一个 .txt 文件,其中包含 70+k json 对象,通过从 twitter 中提取数据并使用以下方法转储到文件中:
with open("followers.txt", 'a') as f:
for follower in limit_handled(tweepy.Cursor(api.followers, screen_name=account_name).pages()):
for user_obj in follower:
json.dump(user_obj._json, f)
f.write("\n")
当我尝试使用以下代码在 python 中阅读此内容时:
import json
with open('followers.txt') as json_data:
follower_data = json.load(json_data)
我得到错误:
ValueError: Extra data: line 2 column 1 - line 2801 column 1 (char 1489 - 8679498)
当我使用上面相同的代码读取包含一个从原始文件复制的 json 对象的测试文件时,它起作用了。一旦我向这个文件添加了第二个 json 对象,然后使用上面相同的代码就会出现错误:
ValueError: Extra data: line 2 column 1 - line 2 column 2376 (char 1489 - 3864)
如何读取包含多个 json 对象的文件?
【问题讨论】:
-
你写的不正确,所以你无法正确阅读。
标签: python json twitter tweepy