gpt4 book ai didi

file - Unicode解码错误: invalid continuation byte when trying to read in document

转载 作者:行者123 更新时间:2023-12-03 06:01:31 25 4
gpt4 key购买 nike

我正在尝试读入包含产品数据的文档并打印某些产品的数据。问题是,我似乎无法无错误地读取它。我只是想打印前 100 个字符,以便将其读入,这样我就可以弄清楚我具体需要打印什么以及如何将其从文件中取出。但我在读它时卡住了。该文档是 UTF-8 格式的,或者应该是......我错过了什么?

这是我的代码:

products = open('products.csv')
productsread = products.read()
print(productsread[:100])

这是我得到的回溯:

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

最佳答案

如果您阅读该文档并且它抛出 UTF-8 编解码器错误,那么它不是 UTF-8,或者至少其中有错误。 open('products.csv',encoding='utf8',errors='replace') 将使用 Unicode 代码点 U+FFFD REPLACEMENT CHARACTER 替换所有错误,但确保您的大部分文档实际上 UTF-8。

关于file - Unicode解码错误: invalid continuation byte when trying to read in document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46623798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com