以下代码应该可以正常工作,但是我仍然收到相同的编码错误。我已经尝试了一些解决方案,但无济于事。尝试的解决方案已在下面注释。
代码在这里:
MyStreamer(TwythonStreamer)类:
# Received data
def on_success(self,data):
# Only collect tweets in English
if data['lang'] == 'en':
tweet_data = process_tweet(data)#ATTEMPTED SOLUTION 1#.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
self.save_to_csv(tweet_data)
# Problem with the API
def on_error(self,status_code,data):
#ATTEMPTED SOLUTION 2# with io.open(data,'w',encoding='utf-8') as f:
print(status_code, data)
#ATTEMPTED SOLUTION 3# codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
self.disconnect()
# Save each tweet to csv file
def save_to_csv(self,tweet):
with open(r'saved_tweets.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(list(tweet.values()))