使用Python的编码工具来处理不同编码的文件。
例如,如果文件的编码是UTF-16LE,则可以使用下面的代码:
with open('file.txt', encoding='utf-16le') as f:
content = f.read()
如果文件的编码无法确定,可以尝试使用chardet模块来自动检测编码:
import chardet
with open('file.txt', 'rb') as f:
encoding = chardet.detect(f.read())['encoding']
with open('file.txt', encoding=encoding) as f:
content = f.read()