python中g(shù)bk轉(zhuǎn)utf8 python怎么把ASNI(GB2312)轉(zhuǎn)換成UTF-8?
python怎么把ASNI(GB2312)轉(zhuǎn)換成UTF-8?#!/usr/bin/env python3from chardet import detectfn=“a.txt”with open(fn
python怎么把ASNI(GB2312)轉(zhuǎn)換成UTF-8?
#!/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!”!將編碼轉(zhuǎn)換為UTF-8并將內(nèi)容連接到{}'。Format(newf))
如果您報告importerror,那么
PIP install chardet
您可以在理解代碼后集成def