小编pin*_*hon的帖子

UnicodeDecodeError:尝试读取文档时无效的连续字节

我试图读入包含产品数据的文档并打印出某些产品的数据。问题是,我似乎无法毫无错误地读取它。我只是想打印前 100 个字符来读取它,这样我就可以找出我需要打印的具体内容以及如何将其从文件中提取出来。但我一直在读它。文档是 UTF-8,或者应该是……我错过了什么?

这是我的代码:

products = open('products.csv')
productsread = products.read()
print(productsread[:100])
Run Code Online (Sandbox Code Playgroud)

这是我得到的回溯:

Traceback (most recent call last):
File "nilescratchpad.py", line 2, in <module>
productsread = products.read()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 7451: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)

unicode decode file traceback

4
推荐指数
1
解决办法
4670
查看次数

标签 统计

decode ×1

file ×1

traceback ×1

unicode ×1