Python3批量转换文本文件编码
Python3批量转换文本文件编码
#-*- coding: utf-8 -*-
try:from chardet.universaldetector import UniversalDetectorIsAuto = True except ImportError:IsAuto = False import os import os.path import globdef Convert_Auto( filename,out_enc="utf-8"): ''' Re-encode text file with auto detec current encode. Need chardet Lib. Input Parameter:filename: full path and file name, e.g. c:dir1file.txtout_enc: new encode. Default as 'utf-8' Output Parameter None''' try: f=open(filename,'rb')b= b' ' b+=f.read(1024) u=UniversalDetector() u.reset() u.feed(b) u.close() f.seek(0) b=f.read() f.close() in_enc=u.result['encoding']new_content=b.decode(in_enc, 'ignore')f=open(filename, 'w', encoding=out_enc) f.write(new_content) f.close()print ("Success:"+filename+"converted from"+ in_enc+"to"+out_enc +"!")except IOError: print ("Error:"+filename+"FAIL to converted from"+ in_enc+"to"+out_enc+"!")def Convert_Manu( filename,in_enc='gbk', out_enc="utf-8"): ''' Re-encode text file with manual decide input text encode. Input Parameter:filename: full path and file name, e.g. c:dir1file.txtin_enc: current encode. Default as 'gbk'out_enc: new encode. Default as 'utf-8' Output Parameter None''' try:print ("convert"+ filename) f=open(filename,'rb') b=f.read() f.close()new_content=b.decode(in_enc, 'ignore')f=open(filename, 'w', encoding=out_enc) f.write(new_content) f.close()print ("Success:"+filename+"converted from"+ in_enc+"to"+out_enc +"!")except IOError: print ("Error:"+filename+"FAIL to converted from"+ in_enc+"to"+out_enc+"!")def explore(dir, IsLoopSubDIR=True):'''Convert files encoding. Input:dir : Current folderIsLoopSubDIR: True -- Include files in sub folderFalse-- Only include files in current folder Output: NONE '''if IsLoopSubDIR:flist=getSubFileList(dir, '.txt') else:flist=getCurrFileList(dir, '.txt')for fname in flist:if IsAuto:Convert_Auto(fname, 'utf-8') else:Convert_Manu(fname, 'gbk', 'utf-8')def getSubFileList(dir, suffix=''):'''Get all file list with specified suffix under current folder(Include sub folder) Input:dir : Current foldersuffix : default to blank, means select all files. Output:File list ''' flist=[]for root, dirs, files in os.walk(os.getcwd()):for name in files:if name.endswith(suffix):flist.append(os.path.join(root, name))return flistdef getCurrFileList(dir, suffix=''):'''Get all file list with specified suffix under current level folder Input:dir : Current foldersuffix : default to blank, means select all files. Output:File list '''if suffix=='': files=glob.glob('*') else: files=glob.glob('*'+suffix) flist=[]for f in files:flist.append(os.path.join(os.getcwd(), f))return flistdef main(): explore(os.getcwd(), True)if __name__ =="__main__": main()
Python3批量转换文本文件编码相关推荐
- 一个批量转换文本文件编码的程序(Python)
一个批量转换文本文件编码的程序(Python) #!/usr/bin/python import os,sys def convert( filename, in_enc = "GBK&qu ...
- powershell 批量转换文本文件编码(GBK转UTF-8)
powershell 批量转换文本文件编码(GBK转UTF-8) 01 前言 02 正文 03 后记 01 前言 手头有一批SQL文件,通过某程序批量更新到Local DB.但是发现导进去后中文变乱码 ...
- 批量修改文本文件编码GB18030为UTF-8
批量修改文本文件编码GB18030为UTF-8 [root@localhost Desktop]# ./digui-gb18030-utf8.sh experiment digui-gb18030-u ...
- shell 批量转换文件编码
相信大家在平时的跨平台编程中碰到过文件编码问题,比如在Windows代码字符编码方式是GB2312,然而转到Linux却只支持utf-8,虽然对代码部分没啥影响,但是很多中文注释部分,却一片乱码,很让 ...
- 使用 windows命令和iconv.exe批量转换文件编码
使用 windows命令和iconv.exe批量转换文件编码 iconv是知名的开源跨平台编码转换库,iconv.exe是iconv库在windows下的命令行工具,iconv.exe的一般用法 ...
- 使用windows命令和iconv.exe批量转换文件编码
使用windows命令和iconv.exe批量转换文件编码 iconv是知名的开源跨平台编码转换库,iconv.exe是iconv库在windows下的命令行工具,iconv.exe的一般用法:ico ...
- python实现批量转换文件编码(批转换编码示例)
这篇文章主要介绍了python实现批量转换文件编码示例,指定文件编码.目录或扩展名即可进行转换,大家参考使用吧 # -*- coding:utf-8 -*- __author__ = 'walksky ...
- python批量转换文件编码
python批量转换文件编码 3年之前 python 今天在 eclipse 中导入了个之前的 swing 项目,结果跑起来后乱码,检查代码发现竟然一部分 java 文件是 utf-8 编码, ...
- 一个批量转换文件编码的python脚本
一个批量转换文件编码的python脚本 需要将工作目录下的文件进行转码,开始的编码是GBK的,需要将其转换为utf-8的.文件较多,手动转换肯定不行,用Python写个脚本来实现.找到一段代码参考: ...
最新文章
- sysbench0.5 mysql_sysbench 0.5 安装及 MySQL 基准测试
- ​Unity 2D游戏开发教程之2D游戏的运行效果
- VS Code 的常用快捷键和插件
- 大学计算机2级考证计划书,计算机二级培训计划书..doc
- 【BZOJ-3033】太鼓达人 欧拉图 + 暴搜
- NSString 中包含中文字符时转换为NSURL
- sql max同一行_超经典SQL题 | 做完这4道面试题你就过关了
- [转]上海五年的奋斗历程 从月薪3500到700万(感人!催人振奋!)
- sqlserver高可用方案_PowerJob 的自实现高可用方案,妙妙妙
- 计算机主机放电,电脑需要放电才能开机_电脑主板放电才能开机
- matlab-基础 取整函数 向0取整 取最近整数 向上取整 向下取整
- 关于Base32和Base64的一点区分注意
- 计算机突然找不到u盘了,win10系统u盘文件突然不见了怎么恢复
- 解决手机QQ或微信已开通邮件提醒,但收到邮件仍不提醒的问题
- WPF随笔(七)--分页控件
- 11,SFDC 管理员篇 - 报表和数据的可视化
- 运营商SGSN与GGSN作用
- [vue]vue接入AntV G2Plot
- 90-95年CPU功耗感知调度研究
- Python学习demo01 创建牌->洗牌->发牌->整理牌
热门文章
- Oauth协议是否会泄露用户的密码
- frame页面跳转和信息提示页面
- Android sqlite数据库操作通用框架AHibernate(一)-CRUD示例和使用步骤
- 【iOS QR Code】集成ZXingWidget(XCode Version 4.5.2,iOS 6.0 SDK)
- 开发一款即时通讯App,从这几步开始
- Win7下 OpenCV+Qt开发环境搭建
- 如何检测被锁住的Oracle存储过程及处理办法汇总(转)
- krpano漫游加方向性3D声音(这篇文章已被移到krpano中国网站 krpano360.com)
- 一个好习惯可以让自己享受一辈子
- 引用的一个JS组件,JCombo 0.9