这个“简单易用”的代码示例会让你开始..你可以根据需要调整它。请注意,它会逐行处理文件,因此可以处理任何大小的文件。
start_marker = 'startdel'
end_marker = 'enddel'
with open('data.txt') as inf:
ignoreLines = False
for line in inf:
if start_marker in line:
print line,
ignoreLines = True
if end_marker in line:
ignoreLines = False
if not ignoreLines:
print line,
它采用startdel
和enddel
为“标记”的开始和结束数据的忽略。
更新:
基于评论的请求修改后的代码,这将现在包括/打印包含“标志”的行。
鉴于这种输入数据(来自@drewk借用):
Beginning of the file...
stuff
startdel
delete this line
delete this line also
enddel
stuff as well
the rest of the file...
它产生:
Beginning of the file...
stuff
startdel
enddel
stuff as well
the rest of the file...
这将有助于确定哪些是“MATCH1”和“MATCH2”,也为您的显示输入一些相应的输出。还有其他任何约束(也可以通过输入/输出对来显示)。 – Levon 2012-08-09 13:16:53