mofdan 发表于 2017-5-7 14:13:13

python处理非utf8编码文件转为utf8

  1 判断文件的编码

import chardet
def check_file_charset(file):
with open(file,'rb') as f:
return chardet.detect(f.read())   
return {}
  2 编码转换

            import codecs
from django.utils.encoding import smart_text
f_type = check_file_charset(file_path)
if f_type and 'encoding' in f_type.keys() and f_type['encoding'] != 'utf-8':
try:
with codecs.open(file_path, 'rb', f_type['encoding']) as f:
content = smart_text(f.read())
with codecs.open(file_path, 'wb', 'utf-8') as f:
f.write(content)
except:
pass
页: [1]
查看完整版本: python处理非utf8编码文件转为utf8