• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python批量转换文件编码格式

Python批量转换文件编码格式

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python批量读取文件,python 批量注释,python批量修改文件名,python 批量下载,python批量裁剪等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

#!/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())
</div>

py2.6以下版本可用代码

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() 
</div>

支持py3.1的版本

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()
</div>

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

</div>

您可能想查找下面的文章:

  • Python批量转换文件编码格式

相关文章

  • Python采用socket模拟TCP通讯的实现方法
  • Django与遗留的数据库整合的方法指南
  • python实现获取Ip归属地等信息
  • python字符串str和字节数组相互转化方法
  • Python yield 小结和实例
  • python使用BeautifulSoup分析网页信息的方法
  • python中ConfigParse模块的用法
  • Python读取图片EXIF信息类库介绍和使用实例
  • Python的Django框架中的数据过滤功能
  • 在Python的Flask中使用WTForms表单框架的基础教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中的with...as用法介绍
    • python 随机数生成的代码的详细分析
    • Python 连连看连接算法
    • python查询sqlite数据表的方法
    • python实用代码片段收集贴
    • python操作mysql中文显示乱码的解决方法
    • python基础教程之循环介绍
    • Python解析xml中dom元素的方法
    • python实现ping的方法
    • 分析Python的Django框架的运行方式及处理流程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有