当用python处理包含中文字符的文本文件时,经常会遇到utf-8无法解码的问题,对于这个问题有两种解决思路:
1,打开文件时声明编码方式,多数中文采用UTF-8,GBK,GB2312,GB18030编码;
2,当不确定文件编码方式时,可借助chardet库(使用pip进行安装)进行编码探测。如(with 语句后面的行都有一次缩进):
import chardet
with open('data.txt','rb') as fd:
raw_data = fd.read(10)
det_result = chardet.detect(raw_data)
encode = det_result['encoding']
print(f'检测出的编码为:{encode}')