UnicodeDecodeError: Codec 'utf8' kann Byte 0x80 an Position 3131 nicht decodieren: ungültiges Startbyte
Ich versuche, Twitter-Daten mit Python 2.7.12 aus einer JSON-Datei zu lesen.
Code, den ich verwendet habe, ist wie folgt:
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def get_tweets_from_file(file_name):
tweets = []
with open(file_name, 'rw') as twitter_file:
for line in twitter_file:
if line != '\r\n':
line = line.encode('ascii', 'ignore')
tweet = json.loads(line)
if u'info' not in tweet.keys():
tweets.append(tweet)
return tweets
Ergebnis habe ich:
Traceback (most recent call last):
File "twitter_project.py", line 100, in <module>
main()
File "twitter_project.py", line 95, in main
tweets = get_tweets_from_dir(src_dir, dest_dir)
File "twitter_project.py", line 59, in get_tweets_from_dir
new_tweets = get_tweets_from_file(file_name)
File "twitter_project.py", line 71, in get_tweets_from_file
line = line.encode('ascii', 'ignore')
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 3131: invalid start byte
Ich habe alle Antworten auf ähnliche Fragen durchgesehen und mir diesen Code ausgedacht, und es hat beim letzten Mal funktioniert. Ich habe keine Ahnung, warum es jetzt nicht funktioniert ... Ich würde mich über jede Hilfe freuen!