Twitterのデータをjson形式で取得してファイルに保存しています。TypeError: 'str'ではなくバイトのようなオブジェクトが必要です - PythonでJSONデータを保存する
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
しかし、私は以下のエラーを取得しています:
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'
このコードはこのエラーを生成できません。 –
@GhilasBELHADJ:できることを確認してください。 –
なぜバイナリモードでファイルを開いたのですか?実際には、 'file.close()'メソッドを呼び出すのではなく、その行はそれほど冗長ではありません。 –