因工作需要,要把android项目从Eclipse转到Android Studio,然后想通过Python进行批量转码,但是转码后出现中文乱码。
使用的python版本是2.7,chardet是官网下载的,系统是window7
项目默认的编码格式是GBK,但chardet.detect判断却是gb2312
def convert(filename, in_enc = ["ASCII","GB2312","GBK","gb18030"], out_enc = "UTF-8"):
try:
print "convert " + filename
content = open(filename).read()
result = chardet.detect(content)
coding = result.get("encoding")
for k in in_enc:
if k == coding:
print coding + " To UTF-8"
new_content = content.decode(coding).encode(out_enc)
open(filename,'w').write(new_content)
print "done."
break;
except IOError,e:
print " error"
GBK 是 GB2312 的超集,当字符在 GBK 集合中,但不在 GB2312 时,就会乱码。
当 chardet.detect 识别出 GB2312 时,直接用 GBK 或者 GB18030 decode 即可。