Python3 阅读时无法解码某些字符

Python 3 can't decode certain characters when readin

我有一些超级简单的代码试图打开一个文件,但它包含一些 Chinese/Arabic 个字符,我认为这些字符阻止我打开它。我不确定如何修改文件以允许它打开这些字符。我的代码很简单

a_file = open("test2.txt")
lines = a_file.readlines()
print(lines)

我的错误信息是

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2948: character maps to <undefined>

我该如何解决这个问题?谢谢!

错误信息

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2948: character maps to

告诉您文件中的字节无法使用系统的默认编码解码(“'charmap' 编解码器无法解码”消息通常出现在 Windows 系统上使用 legacy 8-bit encodings.)

如果文件包含中文或阿拉伯字符,打开文件时使用的正确编码更有可能是 UTF-8 或 UTF-16。

请注意,ISO-5589-1 / latin-1 编码将解码任何字节,但结果可能没有意义,因为它是一种只能表示 256 个字符的 8 位编码。

>>> s = '你好,世界'
>>> bs = s.encode('utf-8')
>>> print(bs.decode('ISO-8859-1'))
你好ï¼ä¸ç