Replace 如何在最后一次出现搜索字符串后在行中插入文本?
这件事我已经绞尽脑汁好几个小时了 我正在尝试编写一个脚本,该脚本将在一组源*.cpp文件中的现有include文件最后一次出现后插入一个额外的include文件和注释。源文件位于一组递归的目录中,因此我想我的Scrut必须从查找开始 例如,在:Replace 如何在最后一次出现搜索字符串后在行中插入文本?,replace,insert,find,Replace,Insert,Find,这件事我已经绞尽脑汁好几个小时了 我正在尝试编写一个脚本,该脚本将在一组源*.cpp文件中的现有include文件最后一次出现后插入一个额外的include文件和注释。源文件位于一组递归的目录中,因此我想我的Scrut必须从查找开始 例如,在: #include <a> #include <b> #include <c> // source code... #包括 #包括 #包括 //源代码。。。 之后: #include <a> #inclu
#include <a>
#include <b>
#include <c>
// source code...
#包括
#包括
#包括
//源代码。。。
之后:
#include <a>
#include <b>
#include <c>
// This is the extra include file
#include <d>
// source code...
#包括
#包括
#包括
//这是额外的包含文件
#包括
//源代码。。。
你的问题很模糊。所以我会把你需要做的事情分解
你可以使用C++来实现,并且Boost(以及C++ 11)都有你需要的所有抽象。p> 您可以在python中执行此操作,我在下面制作了一个示例脚本:
import os
def find_last_include(file_name):
""" Returns last line with an include statement at the start """
last_include_line = 0
with open(file_name, "r") as f:
for i,line in enumerate(f):
if line.strip().startswith("#include"):
last_include_line = max(i, last_include_line)
return last_include_line
def insert_line(file_name, last_include_line_no, new_line):
""" New line should end with \n"""
try:
with open(file_name,"r+") as f:
print "Opening: {0}".format(file_name)
# File is all the lines in the file as a list
file = f.readlines()
# last include line is the line we are going to replace the last inculde
# with the last include + the new line we want
last_include_line = file[last_include_line_no] + new_line
file[last_include_line_no] = last_include_line
print "Inserting: '{0}' On line: {1}".format(new_line.strip(), last_include_line_no)
f.seek(0) # Seek back to the start of the file
for line in file:
f.write(line) # Write the lines with changes to the file
except IOError as e:
print e
return None
if __name__ == '__main__':
c_files = find_all(".c","Y:\\Python Stuff")
line = "#include <c>\n"
for c_file in c_files:
insert_line(c_file, find_last_include(c_file), line)
print "Finished Inserting lines"
导入操作系统
def find_last_include(文件名):
“”“返回最后一行,开头有一个include语句”“”
最后一条包含线=0
打开(文件名为“r”)作为f:
对于i,枚举(f)中的行:
如果line.strip().startswith(“#include”):
最后一条包含线=最大值(i,最后一条包含线)
返回最后一个包含行
def插入行(文件名、最后包含行、新行):
“”“新行应以\n结尾”“”
尝试:
打开(文件名为“r+”)作为f:
打印“打开:{0}”。格式(文件名)
#文件是文件中所有行的列表
file=f.readlines()
#最后一个include行是我们要替换最后一个inculde的行
#最后一个include+是我们想要的新产品线
last_include_line=文件[last_include_line_no]+新行
文件[last_include_line_no]=last_include_line
打印“在第{1}行插入:{0}”。格式(new_line.strip(),last_include_line_no)
f、 查找(0)#查找回文件的开头
对于文件中的行:
f、 写入(行)#将更改后的行写入文件
除IOE错误外:
打印e
一无所获
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
c_files=find_all(“.c”,“Y:\\Python Stuff”)
行=“#包括\n”
对于c_文件中的c_文件:
插入_行(c_文件,查找最后一个_包含(c_文件),行)
打印“已完成插入行”
这张照片是:
SEARCHING FOR FILES..
FOUND FILE: Y:\Python Stuff\prog.c
Finished finding
Opening: Y:\Python Stuff\prog.c
Inserting: #include <c> On line: 34
Finished Inserting lines
搜索文件。。
找到的文件:Y:\Python Stuff\prog.c
完成查找
开始:Y:\Python Stuff\prog.c
插入:#包括第34行
已完成插入行
这样做的目的是查找从给定文件夹开始的所有“.c”文件,然后查找最后一个include所在的行,并将该行添加到该行,然后重写该文件。工作起来很有魅力。任何改进意见都将不胜感激。我相信你现在已经发现,这个问题的困难部分是“上次发生”之后。这让我想起了我的祖父,他在同一个城镇住了87年,偶尔会给出类似“我们离Main St的最后一个加油站只有一个街区。”除非你对Main St.有足够的了解,知道最后一个加油站是76号,否则你可能会在下一个镇上犹豫是否还有另一个加油站 那么解决方案是什么呢?理论上,include的最后一次出现可能是文件的最后一行。因此,您需要进行第一次遍历,并将整个文件读取到内存中,以跟踪最后一个include行发生的位置。然后再重复一遍,写出每一行,直到最后一次出现,写出附加的include,然后写出其余的行 我相信,在流媒体庄园中有很多方法可以做到这一点,但它们仍然需要两次通过您的数据。另一方面,这显然是一个源代码文件,这意味着它非常小,很容易放入内存
但是,了解一些典型的源代码模式后,您可能可以找到第一个出现的不是
include…
的行,并在其前面插入新的include 我认为这个问题意味着他不知道什么是,只是想找到文件中的最后一行include。他需要阅读整个文件,然后在知道最后一个include的实际位置后返回并添加新行。oo可以做到这一点,必须重新考虑我的strat@noelkd:是,#include表示包含任何文件,无论是在尖括号内还是引号内。还是谢谢你。@noelkd:这就是我要找的。非常感谢,很抱歉回复得太晚。你所描述的那个角落案例我甚至都不会考虑。这意味着没有任何编码约定。