我在Jupyter Notebook中使用Python (3.4),默认情况下它应该已经使用utf-8了。但是当我从csv文件中读取一些数据时,我得到了以下错误。你知道怎么解决这个问题吗?谢谢!
从csv文件读取的代码:
with open('my_data.csv') as csvfile:
line = csv.reader(csvfile)
for row in line:
print (row)错误:
Last executed 2017-01-06 04:58:59 in 85ms
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-19-09c617346fbb> in <module>()
14 line = csv.reader(csvfile)
15
---> 16 for row in line:
17 print (row)
/usr/lib/python3.4/encodings/ascii.py in decode(self, input, final)
24 class IncrementalDecoder(codecs.IncrementalDecoder):
25 def decode(self, input, final=False):
---> 26 return codecs.ascii_decode(input, self.errors)[0]
27
28 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 2588: ordinal not in range(128)发布于 2017-01-09 03:32:17
指定打开文件时的编码。对于每个csv module documentation,还要对要包装的文件使用newline=''选项:
with open('my_data.csv',encoding='utf8',newline='') as csvfile:
line = csv.reader(csvfile)https://stackoverflow.com/questions/41499256
复制相似问题