使用Python的'chardet'库检测文本编码,并使用正确的编码打开文件或使用正确的编码解析字符串。示例代码如下:
import chardet
with open('file.txt', 'rb') as f:
content = f.read()
encoding = chardet.detect(content)['encoding']
content = content.decode(encoding)
print(content)
该代码会打开一个名为 'file.txt' 的文件,并通过chardet检测文件的编码,然后使用正确的编码解析文件内容。这样就可以正确地看到文本内容并避免出现乱码。