我正在尝试在一个非常大的 .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编码错误吗?如果是,我如何知道正确的编码方式?
谢谢并致以最好的问候!
file -bi [your_filename]
的输出。您将获得一个编码。之后将encoding
参数提供给open()
。 - user5164080