python脚本替换指定行实现步骤
本文主要介绍了Python的脚本替换,由于工作的需要,必须对日志系统进行更新,这里在网上搜索到一篇文章比较不错,这里记录下,大家可以参考下,
工作中需要迁移代码,并把原来的日志系统更新到现在的格式,原来获取log的格式是
AuctionPoolLoggerUtil.getLogger()
现在获取log的格式是:
LoggerFactory.getLogger(XXXXX.class)
这里的XXXXX需要替换为当前的类名。如果这样的java文件不多还好,可以一个个人肉替换。一旦这样的文件很多,特别是迁移过来大量的文件时,你就会发现简直是一场灾难。其实我们发现上面的工作很多是机械单调的。ide中的替换功能不能做到的是把XXXXX替换成当前的类名。而python很容易处理文本,利用正则表达式可以比较方便的拿到类名,然后替换掉xxxxx就可以了。
实现代码:
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"(") 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 后面需要有, 否则会出现多余的空行 print line.replace('XXXXX',className), else: print line, def search(): os.path.walk(packDir,processDirectory,None) if __name__ == '__main__': search()
上面的脚本中大部分是fileinput.input的注释,就是说了inplace=1其实就是把源文件的内容放到缓存区,然后直接将内容写入源文件
findClassNameP 是查找class name的正则表达式,上面的逻辑就是对文件逐行分析,拿到class name。然后再分析当前行是否有xxxxx,有的话就用class name 替换,没有的话就原行输出。
以上使用对python脚本替换指定行的简单实例,如果大家有疑问或者更好的方法可以留言讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 群星《这座城市的歌》2CD[DTS-WAV]
- 大自然音乐系列《缤纷四季》[WAV/CUE/分轨][567.9MB]
- 《宝岛情歌·甜歌声声醉》黑胶2CD[WAV+CUE][900MB]
- 《十四首发烧友公认的好歌 微风细雨》[WAV+CUE][410MB]
- 群星.2024-小财迷影视剧原声带【星辰盛想】【FLAC分轨】
- 群星.1994-宝丽金主题曲巡礼【宝丽金】【WAV+CUE】
- 胡杨林.2011-爱上了瘾(EP)【喜欢音乐】【WAV+CUE】
- 仙境传说新启航2024公测可用礼包码大全 仙境传说兑换码礼包大全
- 魔兽世界地心之战神牧用什么食物合剂 地心之战神牧食物合剂推荐
- 魔兽世界地心之战神牧用什么附魔宝石 地心之战神牧附魔宝石推荐
- 明达年度发烧碟MasterSuperiorAudiophile2019[DSF]
- 明达年度发烧碟MasterSuperiorAudiophile2020[DSF]
- 【发烧唱片】Naim《示范碟(第二辑)》1999[WAV+CUE]
- 《优米雅的炼金工房》“妮娜”战斗实机:大雷黑丝金发御姐
- 许冠杰.2003-四合一珍藏集4CD【宝丽金】【WAV+CUE】