python腳本替換指定行實現(xiàn)步驟
python腳本替換指定行實現(xiàn)步驟
本文主要介紹了Python的腳本替換,由于工作的需要,必須對日志系統(tǒng)進行更新,這里在網(wǎng)上搜索到一篇文章比較不錯,這里記錄下,大家可以參考下,
工作中需要遷移代碼,并把原來的日志系統(tǒng)更新到現(xiàn)在的格式,原來獲取log的格式是
AuctionPoolLoggerUtil.getLogger()
現(xiàn)在獲取log的格式是:
LoggerFactory.getLogger(XXXXX.class)
這里的XXXXX需要替換為當前的類名。如果這樣的java文件不多還好,可以一個個人肉替換。一旦這樣的文件很多,特別是遷移過來大量的文件時,你就會發(fā)現(xiàn)簡直是一場災難。其實我們發(fā)現(xiàn)上面的工作很多是機械單調的。ide中的替換功能不能做到的是把XXXXX替換成當前的類名。而python很容易處理文本,利用正則表達式可以比較方便的拿到類名,然后替換掉xxxxx就可以了。
實現(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 后面需要有, 否則會出現(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其實就是把源文件的內容放到緩存區(qū),然后直接將內容寫入源文件
findClassNameP 是查找class name的正則表達式,上面的邏輯就是對文件逐行分析,拿到class name。然后再分析當前行是否有xxxxx,有的話就用class name 替換,沒有的話就原行輸出。
以上使用對python腳本替換指定行的簡單實例,如果大家有疑問或者更好的方法可以留言討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
python 實現(xiàn)矩陣上下/左右翻轉,轉置的示例
今天小編就為大家分享一篇python 實現(xiàn)矩陣上下/左右翻轉,轉置的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
基于Python實現(xiàn)網(wǎng)頁文章轉PDF文檔
有時候看到一篇好的文章,想去保存下來,傳統(tǒng)方式一般是收藏書簽、復制粘貼到文檔或者直接復制鏈接保存,但這也太麻煩了。本文將用Python語言實現(xiàn)將網(wǎng)上的文章轉存為PDF文檔,保存電腦上慢慢看2022-05-05
Windows和Linux下Python輸出彩色文字的方法教程
這篇文章主要介紹了在Windows和Linux中Python輸出彩色文字的方法,通過設置彩色文字給大家更醒目的效果,文中給出了詳細的介紹和示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
Python實現(xiàn)數(shù)據(jù)地址實體抽取
大家好,本篇文章主要講的是Python實現(xiàn)數(shù)據(jù)地址實體抽取,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
用python按照圖像灰度值統(tǒng)計并篩選圖片的操作(PIL,shutil,os)
這篇文章主要介紹了用python按照圖像灰度值統(tǒng)計并篩選圖片的操作(PIL,shutil,os),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python機器學習使數(shù)據(jù)更鮮活的可視化工具Pandas_Alive
今天我分享大家一款非常棒的動畫可視化工具:Pandas_Alive,它以?matplotlib?繪圖為后端,不僅可以創(chuàng)建出令人驚嘆的動畫可視化,而且使用方法非常簡單。本文詳情如下2021-11-11

