python实现JAVA源代码从ANSI到UTF-8的批量转换方法


Posted in Python onAugust 10, 2015

本文实例讲述了python实现JAVA源代码从ANSI到UTF-8的批量转换方法。分享给大家供大家参考。具体如下:

喜欢用eclipse的大神们,可能一不小心代码就变成ANSI码了,需要转换成utf-8嘛,一个文件一个文件的在Notepad2或者notepad++里面转换么?不,这里有批量转换的程序,python实现,需要的拿去用吧。

ansi2utf8.py:

#-*- coding: utf-8 -*-
import codecs
import os
import shutil
import re
import chardet
def convert_encoding(filename, target_encoding):
 # Backup the origin file.
 shutil.copyfile(filename, filename + '.bak')
 # convert file from the source encoding to target encoding
 content = codecs.open(filename, 'r').read()
 source_encoding = chardet.detect(content)['encoding']
 print source_encoding, filename
 content = content.decode(source_encoding) #.encode(source_encoding)
 codecs.open(filename, 'w', encoding=target_encoding).write(content)
def main():
 for root, dirs, files in os.walk(os.getcwd()):
  for f in files:
   if f.lower().endswith('.java'):
    filename = os.path.join(root, f)
    try:
     convert_encoding(filename, 'utf-8')
    except Exception, e:
     print filename
def process_bak_files(action='restore'):
 for root, dirs, files in os.walk(os.getcwd()):
  for f in files:
   if f.lower().endswith('.java.bak'):
    source = os.path.join(root, f)
    target = os.path.join(root, re.sub('\.java\.bak$', '.java', f, flags=re.IGNORECASE))
    try:
     if action == 'restore':
      shutil.move(source, target)
     elif action == 'clear':
      os.remove(source)
    except Exception, e:
     print source
if __name__ == '__main__':
 # process_bak_files(action='clear')
 main()

把程序拷贝到java源文件所在目录下运行就好了。

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
跟老齐学Python之用while来循环
Oct 02 Python
编写Python爬虫抓取豆瓣电影TOP100及用户头像的方法
Jan 20 Python
Python实现读取SQLServer数据并插入到MongoDB数据库的方法示例
Jun 09 Python
python爬虫 Pyppeteer使用方法解析
Sep 28 Python
python抓取多种类型的页面方法实例
Nov 20 Python
如何卸载python插件
Jul 08 Python
Python爬虫爬取新闻资讯案例详解
Jul 14 Python
在终端启动Python时报错的解决方案
Nov 20 Python
Python数据分析入门之教你怎么搭建环境
May 13 Python
Django集成富文本编辑器summernote的实现步骤
May 31 Python
教你如何使用Python Tkinter库制作记事本
Jun 10 Python
利用Matlab绘制各类特殊图形的实例代码
Jul 16 Python
python用10行代码实现对黄色图片的检测功能
Aug 10 #Python
详解Python中dict与set的使用
Aug 10 #Python
分析并输出Python代码依赖的库的实现代码
Aug 09 #Python
python根据京东商品url获取产品价格
Aug 09 #Python
python制作一个桌面便签软件
Aug 09 #Python
Python 实现简单的电话本功能
Aug 09 #Python
python批量提取word内信息
Aug 09 #Python
You might like
php生成局部唯一识别码LUID的代码
2012/10/06 PHP
php阻止页面后退的方法分享
2014/02/17 PHP
URL编码转换,escape() encodeURI() encodeURIComponent()
2006/12/27 Javascript
火狐浏览器(firefox)下获得Event对象以及keyCode
2008/11/13 Javascript
JAVASCRIPT keycode总结
2009/02/04 Javascript
推荐10个超棒的jQuery工具提示插件
2011/10/11 Javascript
详细分析JavaScript函数定义
2015/07/16 Javascript
js HTML5 Ajax实现文件上传进度条功能
2016/02/13 Javascript
JavaScript导航脚本判断当前导航
2016/07/12 Javascript
移动端js图片查看器
2016/11/17 Javascript
angularjs实现上拉加载和下拉刷新数据功能
2017/06/12 Javascript
微信小程序实现简单跑马灯效果
2020/05/26 Javascript
Vue实现push数组并删除的例子
2019/11/01 Javascript
[00:32]2018DOTA2亚洲邀请赛Secret出场
2018/04/03 DOTA
python sys模块sys.path使用方法示例
2013/12/04 Python
Python设计模式之代理模式实例
2014/04/26 Python
Python压缩和解压缩zip文件
2015/02/14 Python
Python实现的读取文件内容并写入其他文件操作示例
2019/04/09 Python
python面试题小结附答案实例代码
2019/04/11 Python
树莓派与PC端在局域网内运用python实现即时通讯
2019/06/22 Python
利用Python库Scapy解析pcap文件的方法
2019/07/23 Python
Window10下python3.7 安装与卸载教程图解
2019/09/30 Python
基于Python批量生成指定尺寸缩略图代码实例
2019/11/20 Python
关于Python 常用获取元素 Driver 总结
2019/11/24 Python
python数据预处理方式 :数据降维
2020/02/24 Python
浅析pip安装第三方库及pycharm中导入第三方库的问题
2020/03/10 Python
linux系统都有哪些运行级别
2012/04/15 面试题
临床医师专业个人自我评价范文
2013/11/07 职场文书
环保倡议书怎么写
2014/05/16 职场文书
办理房产证委托书
2014/09/18 职场文书
违章停车检讨书
2014/10/21 职场文书
综治维稳工作汇报
2014/10/27 职场文书
乡镇计划生育工作汇报
2014/10/28 职场文书
自荐信格式范文
2015/03/04 职场文书
导游词之清晏园
2019/11/22 职场文书
nginx代理实现静态资源访问的示例代码
2022/07/07 Servers