`
xiaolin0199
  • 浏览: 565761 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics