FarmerLi
4/13/2018 - 5:26 AM

Python encoding convert to utf8

Python encoding convert to utf8

import chardet
import codecs

def convert_file_to_utf8(src_path, dst_path):
    content = codecs.open(src_path, 'r').read()
    source_encoding = chardet.detect(content)['encoding']
    if source_encoding == None:
        print "??",src_path
        return
    print "  ",source_encoding, src_path
    if source_encoding != 'utf-8':
        content = content.decode(source_encoding, 'ignore') #.encode(source_encoding)
        codecs.open(dst_path, 'w', encoding='utf-8').write(content)