首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python-3.4中的UnicodeDecodeError

python-3.4中的UnicodeDecodeError
EN

Stack Overflow用户
提问于 2017-01-06 13:04:37
回答 1查看 293关注 0票数 0

我在Jupyter Notebook中使用Python (3.4),默认情况下它应该已经使用utf-8了。但是当我从csv文件中读取一些数据时,我得到了以下错误。你知道怎么解决这个问题吗?谢谢!

从csv文件读取的代码:

代码语言:javascript
复制
with open('my_data.csv') as csvfile:
    line = csv.reader(csvfile)

    for row in line:
        print (row)

错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-01-09 03:32:17

指定打开文件时的编码。对于每个csv module documentation,还要对要包装的文件使用newline=''选项:

代码语言:javascript
复制
with open('my_data.csv',encoding='utf8',newline='') as csvfile:
    line = csv.reader(csvfile)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41499256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档