您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

Python批量转换文件编码格式

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   需要将工作目录下的文件进行转码,开始的编码是GBK的,需要将其转换为utf-8的。文件较多,手动转换肯定不行,用Python写个脚本来实现。

  自己写的方法,适用于linux,

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #!/usr/bin/python #coding=utf-8 import sys import os, os.path import dircache import commands def add(x,y): return x*y   def trans(dirname): lis = dircache.opendir(dirname) for a in lis: af=dirname+os.sep+a ## print af if os.path.isdir(af): ## print af trans(af) else: ## print af+"encoding="+fi.name ft = commands.getoutput('file -i '+af) ## print ft if a.find('.htm')==-1 and a.find('.xml')==-1 and ft.find('text/')!=-1 and ft.find('iso-8859')!=-1: print 'gbk'+ft+">"+af commands.getoutput('iconv -ficonv -f gbk -t utf-8 -c -o'+""+af+""+af)   trans(os.getcwd())

  py2.6以下版本可用代码

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import os,sys   def convert( filename, in_enc = "GBK", out_enc="UTF8" ): try: print "convert " + filename, content = open(filename).read() new_content = content.decode(in_enc).encode(out_enc) open(filename, 'w').write(new_content) print " done" except: print " error"   def explore(dir): for root, dirs, files in os.walk(dir): for file in files: path = os.path.join(root, file) convert(path)   def main(): for path in sys.argv[1:]: if os.path.isfile(path): convert(path) elif os.path.isdir(path): explore(path)   if __name__ == "__main__": main()

  支持py3.1的版本

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import os import sys import codecs #该程序用于将目录下的文件从指定格式转换到指定格式,默认的是GBK转到utf-8 def convert(file,in_enc="GBK",out_enc="UTF-8"): try: print ("convert " +file) f=codecs.open(file,'r',in_enc) new_content=f.read() codecs.open(file,'w',out_enc).write(new_content) #print (f.read()) except IOError as err: print ("I/O error: {0}".format(err))     def explore(dir): for root,dirs,files in os.walk(dir): for file in files: path=os.path.join(root,file) convert(path)   def main(): for path in sys.argv[1:]: if(os.path.isfile(path)): convert(path) elif os.path.isdir(path): explore(path)   if __name__=="__main__": main()

  以上所述就是本文 的全部内容了,希望大家能够喜欢。