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

作者:ykdsg 时间:2022-03-18 16:53:28 

 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脚本替换指定行的简单实例,如果大家有疑问或者更好的方法可以留言讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/ykdsg/article/details/11999681

标签:python,脚本替换
0
投稿

猜你喜欢

  • IE下修改<p>标签的innerHTML出错

    2007-11-11 10:12:00
  • Python中pip安装非PyPI官网第三方库的方法

    2021-01-20 15:51:18
  • JavaScript贪吃蛇的实现代码

    2023-08-31 04:41:44
  • 跟老齐学Python之关于循环的小伎俩

    2022-07-20 07:03:36
  • Python实现合并excel表格的方法分析

    2022-04-24 21:30:22
  • SQL查询效率-100w数据查询只要1秒

    2008-08-20 18:25:00
  • Python中捕获键盘的方式详解

    2021-06-14 15:23:41
  • 利用ASP从远程服务器上接收XML数据

    2007-08-23 12:49:00
  • Python+requests+unittest执行接口自动化测试详情

    2023-07-30 15:08:37
  • SQL Server 2005 内置工具建审查系统

    2009-01-19 14:24:00
  • Python实现乱序文件重新命名编号

    2021-05-20 09:07:04
  • 轻松掌握 SQL Server 2000数据库的构架

    2009-02-05 15:50:00
  • asp显示字符串前5个文字

    2008-08-10 18:47:00
  • PHP中array_slice函数用法实例详解

    2023-06-20 20:18:04
  • python操作excel之xlwt与xlrd

    2023-02-11 08:05:52
  • Python内置函数详细解析

    2021-08-19 21:03:35
  • Oracle 插入超4000字节的CLOB字段的处理方法

    2009-07-12 18:52:00
  • Python中缓存lru_cache的基本介绍和讲解

    2021-09-30 08:23:08
  • Python读取图片为16进制表示简单代码

    2021-07-24 09:34:15
  • Oracle中返回结果集的存储过程分享

    2012-07-11 15:50:16
  • asp之家 网络编程 m.aspxhome.com