每次读取文件出现不同编码内容,头大。
对应每次文本编码格式不同的读取
bytes = min(32, os.path.getsize(file_path)) raw = open(file_path, 'rb').read(bytes) result = chardet.detect(raw) encoding = result['encoding'] f = open(file_path, "r", encoding=encoding) f_content = f.readlines()
部分文件还是无法正确识别编码
更新ing
# 首先二进制方式打开文件
with open(absPath, 'rb') as frb:
# 检测编码方式
cur_encoding = chardet.detect(frb.read())['encoding']
# 指定文件编码方式
with open(absPath, 'r', encoding=cur_encoding) as fr:
Content = fr.read()
用这个包可以解决编码读取问题



