Python学习笔记 --- 对文本编码进行转换方法2

来源:互联网 发布:电子商务美工就业前景 编辑:程序博客网 时间:2024/05/18 00:53
# -*- coding:utf-8 -*-import codecsimport chardetdef convert_encoding(filename, target_encoding):    content = codecs.open(filename, 'r').read()    source_encoding = chardet.detect(content)['encoding']    if source_encoding != 'utf-8':        print "SRC File ENCode : " + source_encoding + " FileName : " + filename        content = content.decode(source_encoding, 'ignore')        codecs.open(filename, 'w', encoding=target_encoding).write(content)def main():    filename = "/data/sftp/mysftp/upload/20170606/ODS_CMBCTBCT.del"    convert_encoding(filename,'utf-8')if __name__ == '__main__':    main()


此代码可以转换非utf-8格式的包含Ascii, GB2312等


文本转换方法1

原创粉丝点击