#安装包工具
$pip3 install chardet
#直接打开文件,中文显示乱码
>>> import chardet >>> f = open('test.log','rb') >>> data = f.read() >>> data b'Python\xe6\x99\xba\xe8\x83\xbd\xe6\xa3\x80\xe6\xb5\x8b\xe7\xbc\x96\xe7\xa0\x81\xe5\xb9\xb6\xe8\xbd\xac\xe7\xa0\x81\n'
#查看打开文件编码
>>> chardet.detect(data) {'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}
编码为utf8,可信度99
#对文件进行转码
>>> data.decode('utf8') 'Python智能检测编码并转码\n'
中文正常显示