自动探测gbk编码文件并转为utf8
2016-07-21 11:20
337 查看
自动探测gbk编码文件并转为utf8
有时候要将一些部分在windows上的代码在linux上打开,但是因为windows上的默认都是gbk,linux上打开都是乱码。因为只有部分文件是gbk编码的,所以不能粗暴的全部转换,要先对文件编码进行推测,然后才能决定是否要转换。
以下
python代码实现遍历目录下指定类型文件并自动探测编码,然后将推测出gbk、gb2312的文件转为utf8。
import codecs def ReadFile (filePath,encoding): with codecs.open(filePath,"r",encoding) as file: try: content = file.read(); return content except UnicodeDecodeError : return '' def WriteFile(filePath,content,encoding): with codecs.open(filePath,"w",encoding) as file: file.write(content) import chardet def getEncode(filename): with open(filename) as file: result = chardet.detect(file.read()) if result['confidence']>0.9: return result['encoding'] else: print result return '' import os import os.path rootdir = os.path.curdir encoding = ['GBK','GB2312'] for parent,dirnames,filenames in os.walk(rootdir): for filename in filenames: if filename.endswith('.h') or filename.endswith('.cpp'): file = os.path.join(parent,filename) encode = getEncode(file) if encode in encoding: content = ReadFile(file,encode) if content!='': print 'transform ' +filename+ ' from '+ encode+ ' to utf-8' WriteFile(file,content,'utf8') else: if encode =='': encode = 'unknow' if encode !='utf-8' and encode !='ascii': print 'suspicious file' + file + ' by '+encode
代码中使用chardet库来探测文件编码,这个库非自带的系统库。需要自己下载。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法