python脚本替换指定行实现步骤


Posted in Python onJuly 11, 2017

 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"(?<=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 后面需要有, 否则会出现多余的空行 
          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脚本替换指定行的简单实例,如果大家有疑问或者更好的方法可以留言讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
Python编程对列表中字典元素进行排序的方法详解
May 26 Python
Django内容增加富文本功能的实例
Oct 17 Python
python3+requests接口自动化session操作方法
Oct 13 Python
python异步存储数据详解
Mar 19 Python
opencv python 图像轮廓/检测轮廓/绘制轮廓的方法
Jul 03 Python
Python实现图像去噪方式(中值去噪和均值去噪)
Dec 18 Python
python实现修改固定模式的字符串内容操作示例
Dec 30 Python
Keras 加载已经训练好的模型进行预测操作
Jun 17 Python
怎么快速自学python
Jun 22 Python
django和flask哪个值得研究学习
Jul 31 Python
python定义具名元组实例操作
Feb 28 Python
Python实现信息管理系统
Jun 05 Python
Python书单 不将就
Jul 11 #Python
Python编写一个闹钟功能
Jul 11 #Python
python自定义异常实例详解
Jul 11 #Python
详解python中的文件与目录操作
Jul 11 #Python
python 系统调用的实例详解
Jul 11 #Python
python中matplotlib实现最小二乘法拟合的过程详解
Jul 11 #Python
Python基于QRCode实现生成二维码的方法【下载,安装,调用等】
Jul 11 #Python
You might like
PHP设置一边执行一边输出结果的代码
2013/09/30 PHP
php使用array_search函数实现数组查找的方法
2015/06/12 PHP
PHP实现事件机制实例分析
2015/06/26 PHP
PHP中文字符串截断无乱码解决方法
2016/10/10 PHP
JavaScript 拾碎[三] 使用className属性
2010/10/16 Javascript
十个迅速提升JQuery性能让你的JQuery跑得更快
2012/12/10 Javascript
判断及设置浏览器全屏模式
2014/04/20 Javascript
js实现图片拖动改变顺序附图
2014/05/13 Javascript
javascript中DOM复选框选择用法实例
2015/05/14 Javascript
Jquery ajax加载等待执行结束再继续执行下面代码操作
2015/11/24 Javascript
jQuery解决IE6、7、8不能使用 JSON.stringify 函数的问题
2016/05/31 Javascript
Bootstrap导航条可点击和鼠标悬停显示下拉菜单的实现代码
2016/06/23 Javascript
AngularJS中isolate scope的用法分析
2016/11/22 Javascript
javascript实现去除HTML标签的方法
2016/12/26 Javascript
扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框
2017/02/21 Javascript
jQuery+ajax实现批量删除功能完整示例
2019/06/06 jQuery
微信小程序实现列表的横向滑动方式
2020/07/15 Javascript
python 中的列表解析和生成表达式
2011/03/10 Python
Python中使用logging模块代替print(logging简明指南)
2014/07/09 Python
简单介绍Python中的readline()方法的使用
2015/05/24 Python
python下MySQLdb用法实例分析
2015/06/08 Python
Python正则表达式分组概念与用法详解
2017/06/24 Python
python中实现控制小数点位数的方法
2019/01/24 Python
python opencv 图像拼接的实现方法
2019/06/27 Python
Python实现基于SVM的分类器的方法
2019/07/19 Python
Pycharm连接远程服务器过程图解
2020/04/30 Python
python相对企业语言优势在哪
2020/06/12 Python
Python logging模块handlers用法详解
2020/08/14 Python
HTML5中的Web Notification桌面右下角通知功能的实现
2018/04/19 HTML / CSS
PUMA官方商城:世界领先的运动品牌之一
2016/11/16 全球购物
德国运动鞋网上商店:Afew Store
2018/01/05 全球购物
30年同学聚会感言
2014/01/30 职场文书
幼儿园中班下学期评语
2014/04/18 职场文书
优秀毕业生找工作自荐信
2014/06/23 职场文书
学校计划生育责任书
2015/05/09 职场文书
分享Python获取本机IP地址的几种方法
2022/03/17 Python