python3上的UnicodeDecodeError

Eli*_*fee 6 python regex utf-8 decoding

我目前正试图在一个非常大的.txt文件(几百万行文本)上使用一些简单的正则表达式.导致问题的最简单的代码:

file = open("exampleFileName", "r")  
    for line in file:  
        pass
Run Code Online (Sandbox Code Playgroud)

错误消息:

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
Run Code Online (Sandbox Code Playgroud)

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

谢谢和最好的问候!

mic*_*ael 10

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

file = open('exampleFileName', 'r', encoding='latin-1') 
Run Code Online (Sandbox Code Playgroud)