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

 

你可能感兴趣的:(django,python,Linux,python)