這個「簡單易用」的代碼示例會讓你開始..你可以根據需要調整它。請注意,它會逐行處理文件,因此可以處理任何大小的文件。
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