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学习笔记之os模块使用总结
Nov 03 Python
github配置使用指南
Nov 18 Python
详解Python3中yield生成器的用法
Aug 20 Python
Python中operator模块的操作符使用示例总结
Jun 28 Python
Python进行数据提取的方法总结
Aug 22 Python
使用pyecharts在jupyter notebook上绘图
Apr 23 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
Apr 02 Python
Python pygorithm模块用法示例【常见算法测试】
Aug 16 Python
Python编写一个验证码图片数据标注GUI程序附源码
Dec 09 Python
django实现模板中的字符串文字和自动转义
Mar 31 Python
利用Pycharm + Django搭建一个简单Python Web项目的步骤
Oct 22 Python
python可视化 matplotlib画图使用colorbar工具自定义颜色
Dec 07 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彩蛋信息介绍和阻止泄漏的方法(隐藏功能)
2014/08/06 PHP
php获取远程文件的内容和大小
2015/11/03 PHP
CI框架支持$_GET的两种实现方法
2016/05/18 PHP
Kindeditor编辑器添加图片上传水印功能(php代码)
2017/08/03 PHP
PHP封装XML和JSON格式数据接口操作示例
2019/03/06 PHP
用JavaScript编写COM组件的步骤
2009/03/17 Javascript
js下通过prototype扩展实现indexOf的代码
2010/12/08 Javascript
基于Jquery制作的幻灯片图集效果打包下载
2011/02/12 Javascript
在JavaScript中处理数组之reverse()方法的使用
2015/06/09 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
jQuery简单实现iframe的高度根据页面内容自适应的方法
2016/08/01 Javascript
JS弹出新窗口被拦截的解决方法
2016/08/09 Javascript
原生JS实现图片左右轮播
2016/12/30 Javascript
vue中的watch监听数据变化及watch中各属性的详解
2018/09/11 Javascript
vue基于element-ui的三级CheckBox复选框功能的实现代码
2018/10/15 Javascript
vue接入腾讯防水墙代码
2019/05/07 Javascript
Vue实现点击按钮复制文本内容的例子
2019/11/09 Javascript
Node.js API详解之 module模块用法实例分析
2020/05/13 Javascript
vue 判断元素内容是否超过宽度的方式
2020/07/29 Javascript
JavaScript常用工具函数库汇总
2020/09/17 Javascript
python字符串常用方法
2018/06/14 Python
对python sklearn one-hot编码详解
2018/07/10 Python
python得到单词模式的示例
2018/10/15 Python
在Python中输入一个以空格为间隔的数组方法
2018/11/13 Python
Python time库基本使用方法分析
2019/12/13 Python
Tensorflow 实现释放内存
2020/02/03 Python
python 瀑布线指标编写实例
2020/06/03 Python
canvas绘制圆角头像的实现方法
2019/01/17 HTML / CSS
Interflora澳大利亚:同日鲜花速递
2019/06/25 全球购物
Laravel中Kafka的使用详解
2021/03/24 PHP
甜品店创业计划书
2014/08/14 职场文书
代办社保委托书范文
2014/10/06 职场文书
考试作弊检讨书
2014/10/21 职场文书
离婚协议书的范本
2015/01/27 职场文书
中国文明网2015年“向国旗敬礼”活动网上签名寄语
2015/09/24 职场文书
Win11运行cmd提示“请求的操作需要提升”的两种解决方法
2022/07/07 数码科技