python實現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉換方法
本文實例講述了python實現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉換方法。分享給大家供大家參考。具體如下:
喜歡用eclipse的大神們,可能一不小心代碼就變成ANSI碼了,需要轉換成utf-8嘛,一個文件一個文件的在Notepad2或者notepad++里面轉換么?不,這里有批量轉換的程序,python實現(xiàn),需要的拿去用吧。
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的NumPy安裝與入門
這篇文章主要介紹了Python入門教程(三十九)Python的NumPy安裝與入門,NumPy 是一個Python包,它是一個由多維數(shù)組對象和用于處理數(shù)組的例程集合組成的庫,,需要的朋友可以參考下2023-05-05
keras.layers.Conv2D()函數(shù)參數(shù)用法及說明
這篇文章主要介紹了keras.layers.Conv2D()函數(shù)參數(shù)用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Python編程快速上手——瘋狂填詞程序實現(xiàn)方法分析
這篇文章主要介紹了Python瘋狂填詞程序實現(xiàn)方法,結合具體案例形式分析了Python填詞算法相關的文件讀寫、正則匹配、數(shù)據(jù)遍歷等操作技巧,需要的朋友可以參考下2020-02-02
詳解pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例
這篇文章主要介紹了pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

