UnicodeDecodeError:'ascii'编解码器无法解码位置104的字节0xef:序数不在范围内(128)

时间:2018-06-30 15:39:26

标签: python python-2.7

我的代码有问题,所以我想将我的csv文件打印到控制台,它看起来像这样:

| ID  |   Tweet                                         |Prediction | System|   
|-----+-------------------------------------------------+-----------+-------|
| 01  |I had seen a movie "Rugrats" thats so funny�    | Positif   |Positif| 
| 02  |Incredibles 2 was amazingly amazing �           | Positif   |Positif|

我使用表格数据制作了表格,所以这是我的代码:

from tabulate import tabulate    
with open("file.csv","rb") as data:
                x = csv.reader(data)
                docs = []
                for i in x:
                    docs.append(i)
                    print tabulate([i], headers=['ID', 'Tweet', 'Prediction', 'System'])

我认为问题是ASCII ,但是我使用了i.decode('utf-8','ignore'),但是它不起作用。所以我对输出控制台的期望是这样的:

| ID  |   Tweet                                         |Prediction | System|   
|-----+-------------------------------------------------+-----------+-------|
| 01  |I had seen a movie "Rugrats" thats so funny      | Positif   |Positif| 
| 02  |Incredibles 2 was amazingly amazing              | Positif   |Positif|

我该怎么做?

1 个答案:

答案 0 :(得分:0)

可以请您尝试:

res.send

否则,只过滤掉数据中的所有字符,而不是ASCII:

with codecs.open( "file.csv", 'rb', encoding='utf-8' ) as data :