1、安装chardet
2、安装完后,在python安装目录会多出2个文件夹
import chardet
import codecs
def convert_file_to_utf8(filename):
# !!! does not backup the origin file
content = codecs.open(filename, 'r').read()
source_encoding = chardet.detect(content)['encoding']
if source_encoding == None:
print("encoding is None: %s"%filename)
return
print("[%s]--->[%s]: %s"%(filename,source_encoding,'utf-8'))
if source_encoding != 'utf-8':
content = content.decode(source_encoding, 'ignore') .encode("utf-8")
codecs.open(filename, 'w', encoding='utf-8').write(content)
if __name__ == "__main__":
convert_file_to_utf8("D:/test.txt")