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

首頁(yè) > 編程 > Python > 正文

python腳本替換指定行實(shí)現(xiàn)步驟

2020-02-16 01:50:04
字體:
供稿:網(wǎng)友

 python腳本替換指定行實(shí)現(xiàn)步驟

       本文主要介紹了Python的腳本替換,由于工作的需要,必須對(duì)日志系統(tǒng)進(jìn)行更新,這里在網(wǎng)上搜索到一篇文章比較不錯(cuò),這里記錄下,大家可以參考下,

工作中需要遷移代碼,并把原來的日志系統(tǒng)更新到現(xiàn)在的格式,原來獲取log的格式是

AuctionPoolLoggerUtil.getLogger() 

現(xiàn)在獲取log的格式是:

LoggerFactory.getLogger(XXXXX.class) 

這里的XXXXX需要替換為當(dāng)前的類名。如果這樣的java文件不多還好,可以一個(gè)個(gè)人肉替換。一旦這樣的文件很多,特別是遷移過來大量的文件時(shí),你就會(huì)發(fā)現(xiàn)簡(jiǎn)直是一場(chǎng)災(zāi)難。其實(shí)我們發(fā)現(xiàn)上面的工作很多是機(jī)械單調(diào)的。ide中的替換功能不能做到的是把XXXXX替換成當(dāng)前的類名。而python很容易處理文本,利用正則表達(dá)式可以比較方便的拿到類名,然后替換掉xxxxx就可以了。

實(shí)現(xiàn)代碼:

import fileinput import os import re  __author__ = 'ykdsg'  packDir='/Users/ykdsg/svn_workspace/auctionplatform/misc_refactory/auctionplatform/ap-biz/src/main/java/com/yk/misccenter' #查找class name findClassNameP=re.compile(r"(?<=class/s)/w*") findXP=re.compile(r"XXXXX")   def processDirectory(args,dirname,filenames):   # print 'Directory',dirname   for filename in filenames:      if os.path.splitext(filename)[1]=='.java':       # print 'file',filename       fullFileUrl=dirname+ "/"+filename       fileObj=open(fullFileUrl)       className=''       # Optional in-place filtering: if the keyword argument inplace=1 is passed to fileinput.input() or to       # the FileInput constructor, the file is moved to a backup file and standard output is directed to the       # input file (if a file of the same name as the backup file already exists, it will be replaced silently)       # . This makes it possible to write a filter that rewrites its input file in place. If the backup       # parameter is given (typically as backup='.<some extension>'), it specifies the extension for the       # backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted       # when the output file is closed. In-place filtering is disabled when standard input is read.       for line in fileinput.input(fullFileUrl, inplace=1):         matchClass = findClassNameP.search(line)         if matchClass:           className = matchClass.group()         matchX=findXP.search(line)         if matchX:           #print 后面需要有, 否則會(huì)出現(xiàn)多余的空行           print line.replace('XXXXX',className),         else:           print line,   def search():   os.path.walk(packDir,processDirectory,None)  if __name__ == '__main__':   search() 

上面的腳本中大部分是fileinput.input的注釋,就是說了inplace=1其實(shí)就是把源文件的內(nèi)容放到緩存區(qū),然后直接將內(nèi)容寫入源文件

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沭阳县| 外汇| 三河市| 辰溪县| 兴仁县| 尤溪县| 阿荣旗| 郁南县| 宁南县| 堆龙德庆县| 盘山县| 高雄县| 贡嘎县| 寿宁县| 鄯善县| 仙居县| 溧阳市| 三亚市| 科技| 韶关市| 浙江省| 德昌县| 勃利县| 揭阳市| 嵩明县| 紫阳县| 西藏| 太白县| 双峰县| 资兴市| 宁海县| 德昌县| 嘉荫县| 河西区| 砀山县| 山西省| 长丰县| 沂水县| 三都| 静海县| 布尔津县|