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嘛,一個(gè)文件一個(gè)文件的在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)文章
對python實(shí)現(xiàn)二維函數(shù)高次擬合的示例詳解
今天小編就為大家分享一篇對python實(shí)現(xiàn)二維函數(shù)高次擬合的示例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python自動(dòng)化部署工具Fabric的簡單上手指南
這篇文章主要介紹了Python自動(dòng)化部署工具Fabric的簡單上手指南,涵蓋Fabric的安裝、fabric的遠(yuǎn)程操作與維護(hù)等方面,需要的朋友可以參考下2016-04-04
Tensorflow實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)擬合線性回歸
這篇文章主要為大家詳細(xì)介紹了Tensorflow實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)擬合線性回歸,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Python實(shí)現(xiàn)的多線程http壓力測試代碼
這篇文章主要介紹了Python實(shí)現(xiàn)的多線程http壓力測試代碼,結(jié)合實(shí)例形式分析了Python多線程操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實(shí)現(xiàn)
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實(shí)現(xiàn),具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10

