python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法
本文實(shí)例講述了python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法。分享給大家供大家參考。具體如下:
喜歡用eclipse的大神們,可能一不小心代碼就變成ANSI碼了,需要轉(zhuǎn)換成utf-8嘛,一個文件一個文件的在Notepad2或者notepad++里面轉(zhuǎn)換么?不,這里有批量轉(zhuǎn)換的程序,python實(shí)現(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()
把程序拷貝到j(luò)ava源文件所在目錄下運(yùn)行就好了。
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
numpy使用技巧之?dāng)?shù)組過濾實(shí)例代碼
這篇文章主要介紹了numpy使用技巧之?dāng)?shù)組過濾實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02Python入門教程(三十九)Python的NumPy安裝與入門
這篇文章主要介紹了Python入門教程(三十九)Python的NumPy安裝與入門,NumPy 是一個Python包,它是一個由多維數(shù)組對象和用于處理數(shù)組的例程集合組成的庫,,需要的朋友可以參考下2023-05-05keras.layers.Conv2D()函數(shù)參數(shù)用法及說明
這篇文章主要介紹了keras.layers.Conv2D()函數(shù)參數(shù)用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Python編程快速上手——瘋狂填詞程序?qū)崿F(xiàn)方法分析
這篇文章主要介紹了Python瘋狂填詞程序?qū)崿F(xiàn)方法,結(jié)合具體案例形式分析了Python填詞算法相關(guān)的文件讀寫、正則匹配、數(shù)據(jù)遍歷等操作技巧,需要的朋友可以參考下2020-02-02詳解pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實(shí)例
這篇文章主要介紹了pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02