小编Rit*_*esh的帖子

类型错误:需要一个类似字节的对象,而不是“str”——在 Python 中保存 JSON 数据

我正在以 json 格式从 twitter 获取数据并将其存储在文件中。

consumer_key = 'Consumer KEY'
consumer_secret = 'Secret'
access_token = 'Token'
access_secret = 'Access Secret'

auth = OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

os.chdir('Path')
file = open('TwData.json','wb')

for status in tweepy.Cursor(api.home_timeline).items(15):
    simplejson.dump(status._json,file,sort_keys = True)
file.close
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/abc/anaconda/lib/python3.6/json/__init__.py", line 180, in dump
    fp.write(chunk)
TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)

python json typeerror

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

json ×1

python ×1

typeerror ×1