首页 > python3 如何批量将gbk格式文本转换为unicode或者utf8格式

python3 如何批量将gbk格式文本转换为unicode或者utf8格式

在用python3处理文本文件时,出现因gbk格式而产生的错误,如何使用python在预处理文件的时候转换gbk文本编码格式为unicode或utf8,或者是否有更优的处理方法?谢谢!


unicode_string=gbk_bytes.decode('gbk').encode('utf-8')

另外“因gbk格式而产生的错误”是什么?


如果你的文件不太大的话:

for f in sys.argv[1:]:
  with open(f, encoding='gb18030') as fobj:
    content = fobj.read()
  with open(f, 'w', encoding='utf-8') as fobj:
    fobj.write(content)
【热门文章】
【热门文章】