我有一个以UTF-8编码的文本文件。如果我阅读并打印了文件,则会出现错误:
Traceback (most recent call last):
File "D:/Projects/pyProjects/antiafk/log.py", line 27, in <module>
log()
File "D:/Projects/pyProjects/antiafk/log.py", line 17, in log
lines = f.readlines()
File "C:\Users\blaho\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 4761: character maps to <undefined>
我的文本文件不包含任何特殊字符。
有代码:
lastLine = None
with open(filePath, mode="r", encoding="utf-8", errors="ignore") as f:
while True:
line = f.readline()
if not line:
break
print(line)
lastLine = line
我尝试将其编码为UTF-8,但没有帮助。