从CSV/List读取时发生UnicodeDecodeError:意外的D结尾

2024-07-02 12:06:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试着用一个叫做DeepMoji的东西来给一个充满tweets的csv评分。tweets必须用Unicode编码。我已经能够用一个小的数据集工作,但是对于我拥有超过200000个点的数据集,我收到了以下错误: UnicodeDecodeError:“utf8”编解码器无法解码位置254中的字节0xe2:意外的数据结尾。在

我尝试过的代码和解决方案如下,但给出了同样的错误,有人有什么想法吗?在

TEST_SENTENCES = []
with open('Cleaned_Data3.csv', 'rU') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        TEST_SENTENCES.append(row["Tweet"])
    try:
        [x.encode('utf-8') for x in TEST_SENTENCES]
    except:
        for rows in TEST_SENTENCES: #attempt to fix the problem 
            str=unicode(str, errors='replace')

这是完整的错误代码。在

^{pr2}$

Tags: csv数据csvfileintestfor错误unicode