国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現JAVA源代碼從ANSI到UTF-8的批量轉換方法

2019-11-25 17:05:40
字體:
來源:轉載
供稿:網友

本文實例講述了python實現JAVA源代碼從ANSI到UTF-8的批量轉換方法。分享給大家供大家參考。具體如下:

喜歡用eclipse的大神們,可能一不小心代碼就變成ANSI碼了,需要轉換成utf-8嘛,一個文件一個文件的在Notepad2或者notepad++里面轉換么?不,這里有批量轉換的程序,python實現,需要的拿去用吧。

ansi2utf8.py:

#-*- coding: utf-8 -*-import codecsimport osimport shutilimport reimport chardetdef 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 filenamedef 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 sourceif __name__ == '__main__': # process_bak_files(action='clear') main()

把程序拷貝到java源文件所在目錄下運行就好了。

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉定区| 汝州市| 红安县| 申扎县| 两当县| 巩义市| 永新县| 丽江市| 银川市| 利辛县| 绥江县| 邢台县| 绥中县| 偏关县| 买车| 宣化县| 万盛区| 城口县| 章丘市| 星子县| 和田县| 金溪县| 绥江县| 吉木萨尔县| 三明市| 平江县| 星座| 龙江县| 宿松县| 榆林市| 宿松县| 茶陵县| 宁都县| 林甸县| 兴城市| 彭山县| 广河县| 子长县| 河间市| 康保县| 大名县|