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 相关文章推荐
Tensorflow简单验证码识别应用
May 25 Python
Python数据结构与算法之二叉树结构定义与遍历方法详解
Dec 12 Python
python topN 取最大的N个数或最小的N个数方法
Jun 04 Python
Python基于opencv调用摄像头获取个人图片的实现方法
Feb 21 Python
获取django框架orm query执行的sql语句实现方法分析
Jun 20 Python
python实现大文本文件分割
Jul 22 Python
Python模拟FTP文件服务器的操作方法
Feb 18 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
Mar 06 Python
Python如何使用ElementTree解析xml
Oct 12 Python
详解pycharm连接远程linux服务器的虚拟环境的方法
Nov 13 Python
python实现发送邮件
Mar 02 Python
pycharm安装深度学习pytorch的d2l包失败问题解决
Mar 25 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
《雄兵连》系列首部大电影《烈阳天道》:可能是因为期望值太高了
2020/08/18 国漫
php 备份数据库代码(生成word,excel,json,xml,sql)
2013/06/23 PHP
php采集自中央气象台范围覆盖全国的天气预报代码实例
2015/01/04 PHP
Ext.MessageBox工具类简介
2009/12/10 Javascript
将函数的实际参数转换成数组的方法
2010/01/25 Javascript
异步加载script的代码
2011/01/12 Javascript
Ajax 数据请求的简单分析
2011/04/05 Javascript
javascript日期对象格式化为字符串的实现方法
2014/01/14 Javascript
javascript简单实现图片预加载
2014/12/03 Javascript
Windows系统中安装nodejs图文教程
2015/02/28 NodeJs
JS+CSS实现鼠标经过弹出一个DIV框完整实例(带缓冲动画渐变效果)
2016/03/25 Javascript
深入理解bootstrap框架之入门准备
2016/10/09 Javascript
js入门之Function函数的使用方法【新手必看】
2016/11/22 Javascript
jQuery插件HighCharts绘制简单2D折线图效果示例【附demo源码】
2017/03/21 jQuery
微信小程序之滚动视图容器的实现方法
2017/09/26 Javascript
react redux入门示例
2018/04/19 Javascript
token 机制和实现方式
2020/12/15 Javascript
跨平台python异步回调机制实现和使用方法
2013/11/26 Python
Django中更新多个对象数据与删除对象的方法
2015/07/17 Python
python变量不能以数字打头详解
2016/07/06 Python
使用Python实现简单的服务器功能
2017/08/25 Python
flask中使用蓝图将路由分开写在不同文件实例解析
2018/01/19 Python
python八皇后问题的解决方法
2018/09/27 Python
Python语言检测模块langid和langdetect的使用实例
2019/02/19 Python
Python面向对象之继承原理与用法案例分析
2019/12/31 Python
python 对任意数据和曲线进行拟合并求出函数表达式的三种解决方案
2020/02/18 Python
Python递归求出列表(包括列表中的子列表)的最大值实例
2020/02/27 Python
Python 解决火狐浏览器不弹出下载框直接下载的问题
2020/03/09 Python
泰国综合购物网站:Lazada泰国
2018/04/09 全球购物
美国奢侈品购物平台:Orchard Mile
2018/05/02 全球购物
大学生的网络创业计划书
2013/12/26 职场文书
大学生自我评价范文
2015/03/03 职场文书
乒乓球比赛通知
2015/04/27 职场文书
职场干货:简历中的自我评价应该这样写!
2019/05/06 职场文书
网络新闻该怎么写?这些写作技巧你都知道吗?
2019/08/26 职场文书
Spring Boot项目传参校验的最佳实践指南
2022/04/05 Java/Android