ITeye博客

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

xiaolin0199 · 2015-12-08 13:41 · python · django · Linux · 编程语言 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。