python怎么把ASNIGB2312转换成UTF

更新时间:01-24 教程 由 终止符 分享

python怎么把ASNIGB2312转换成UTF?

#!/usr/bin/env python3from chardet import detectfn = 'a.txt'with open(fn, 'rb') as f: s = f.read()coding = detect(s)['encoding']print('coding: {}'.format(coding))print('content: {}'.format(s.decode(coding).rstrip()))newf = 'b.txt'with open(newf, 'wb') as f: f.write(s.decode(coding).encode('utf8'))print('done!convert coding to utf-8 and wirte content in `{}`'.format(newf))

如果报ImportError那就

pip install chardet

def 你理解代码后自己整合吧

声明:关于《python怎么把ASNIGB2312转换成UTF》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2172453.html