首页 文章

python:unicodeEncodeError:'charpmap' codec can 't encode character ' \ u2026'

提问于
浏览
0

我试着分析一下我从tweeter得到的一些推文,但似乎我有一个编码问题,如果你有任何想法..

import json

#Next we will read the data in into an array that we call tweets.
tweets_data_path = 'C:/Python34/TESTS/twitter_data.txt'

tweets_data = []
tweets_file = open(tweets_data_path, "r")


for line in tweets_file:
    try:
        tweet = json.loads(line)
        tweets_data.append(tweet)
    except:
        continue

print(len(tweets_data))#412 tweets
print(tweet)

我得到了错误:文件“C:\ Python34 \ lib \ encodings \ cp850.py”,第19行,编码返回codecs.charmap_encode(输入,self.errors,encoding_map)[0] unicodeEncodeError:'charpmap'编解码器'' t在位置1345编码字符'\ u2026':字符映射到未定义

在工作中,我没有得到错误,但我有python 3.3,它有所作为,你觉得呢?

    • -编辑

@MarkRamson的评论回答了我的问题

1 回答

  • 2

    您应该在打开文件时指定编码:

    tweets_file = open(tweets_data_path, "r", encoding="utf-8-sig")
    

相关问题