This question already has answers here:
Switching to Python 3 causing UnicodeDecodeError

(3个答案)


3年前关闭。




我目前正在尝试在一个非常大的.txt文件(几百万行文本)上使用一些简单的正则表达式。导致问题的最简单的代码:
file = open("exampleFileName", "r")
    for line in file:
        pass

错误信息:
Traceback (most recent call last):
  File "example.py", line 34, in <module>
    example()
  File "example.py", line 16, in example
    for line in file:
  File "/usr/lib/python3.4/codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 7332: invalid continuation byte

我怎样才能解决这个问题? utf-8编码错误吗?如果是的话,我怎么知道哪一个是正确的?

谢谢,最好的问候!

最佳答案

看来它是无效的UTF-8,您应该尝试使用latin-1编码进行阅读。尝试

file = open('exampleFileName', 'r', encoding='latin-1')

关于python - python3上的UnicodeDecodeError ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39001641/

10-15 19:20