python - 无法解码JSON对象

时间:2017-06-25 07:24:54

标签: python json python-2.7 tweepy

我试图使用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

更新

正如其中一个答案中所建议的,我现在在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)

1 个答案:

答案 0 :(得分:3)

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