我正在尝试使用python 2.7.12从json文件读取twitter数据。我用UnicodeDecodeError:'utf8'编解码器无法解码位置3131中的字节0x80:无效起始字节
代码是这样的:
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
结果我:
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
我通过从类似问题的所有答案,去与这个代码走过来,它的工作最后一次。我不知道为什么它现在不工作...我会很感激任何帮助!
谢谢!!!我只是尝试,但它不工作 - 我试图取代'windows-1252',因为我正在使用mac。我尝试了'拉丁-1'等。或者没关系......?感谢您的详细解释... – wannabhappy
你现在有什么错误? –
代码运行但我得到数据库中所有变量的“NULL”。 当我打开每个json文件并检查时,文件中有tweet。另外,当我要求打印多条推文时,它说我有0条推文...... – wannabhappy