繁体   English   中英

python - 无法解码JSON对象

[英]python - JSON object could not be decoded

我试图使用tweepy挖掘twitter数据并将数据加载到JSON文件中,但是以下代码:

import tweepy
from tweepy import OAuthHandler
import json

consumer_key = '****'
consumer_secret = '****'
access_token = '****'
access_secret = '****'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

f = open('twitterdata.json', 'a+')

for status in tweepy.Cursor(api.home_timeline).items(10):
        json.dump(status._json, f)

line = f.readline()
tweet = json.loads(line)
print json.dumps(tweet, indent = 4)

产生错误:

Traceback (most recent call last):
  File "mytwittermine.py", line 21, in <module>
    tweet = json.loads(line)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

UPDATE

正如其中一个答案中所建议的那样,我现在在for循环的每次迭代中添加一个换行符,所以代码现在是:

import tweepy
from tweepy import OAuthHandler
import json

consumer_key = '****'
consumer_secret = '****'
access_token = '****'
access_secret = '****'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

f = open('twitterdata.json', 'a+')

for status in tweepy.Cursor(api.home_timeline).items(10):
        json.dump(status._json, f)
        f.write('\n')

f.seek(0)
line = f.readline()
tweet = json.loads(line)
print json.dumps(tweet, indent = 4)

上面的代码给出了ValueError:

Traceback (most recent call last):
  File "mytwittermine.py", line 23, in <module>
    tweet = json.loads(line)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 3115 - line 2 column 1 (char 3114 - 301245)

你写入一个文件,然后用倒带读取,所以没有任何东西被读取。 在写和读之间添加f.seek(0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM