渊博自习室 2023-03-20 09:01 采纳率: 0%
浏览 201

'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte


UnicodeDecodeError Traceback (most recent call last)
Input In [9], in <cell line: 2>()
1 filename = "/Users/mac/Documents/网络公开信息的使用/renkou.csv"
2 with open(filename,encoding="utf-8") as fp:
----> 3 for line in fp.readlines():
4 print(line)

File ~/opt/anaconda3/lib/python3.9/codecs.py:322, in BufferedIncrementalDecoder.decode(self, input, final)
319 def decode(self, input, final=False):
320 # decode input (taking the buffer into account)
321 data = self.buffer + input
--> 322 (result, consumed) = self._buffer_decode(data, self.errors, final)
323 # keep undecoded input until the next call
324 self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte

  • 写回答

7条回答 默认 最新

  • lshen01 2023-03-20 09:07
    关注

    参考GPT和自己的思路:

    这个错误的原因是编码格式不正确,因为在读取文件时设定的编码格式为utf-8,但是文件中存在一些无法被解码的字符,这导致了错误的发生。可能的解决方法包括更改文件编码格式为正确的utf-8格式、尝试使用其他编码格式打开文件、或者在读取文件时使用错误处理方式来处理这些无法被解码的字符。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月20日