注:我發帖後意識到,如果最後一次出現是沒有01的最後一行這種技術將會使它和下一次發生一樣。
首先,你需要遍歷文件,直到找到第一次出現:
file = <OPEN FILE>
rep_line = "<this is repeated>\n"
beginning = "" #record all data until found
while True: #broken when rep_line is found in file (or end of file is reached)
line = file.readline()
if not line:
raise EOFError("reached end of file before finding first occurence")
beginning+=line
if line == rep_line:
break
rest = file.read() #you can read the rest after iterating over a few lines
然後,你將有beginning
其中包含的一切直至幷包括第一次出現,而rest
因此,您需要做的所有與rest
是count
如何可能發生時間,並取代所有,但最後一個:
reps = rest.count(rep_line)
new_text = beginning + rest.replace(rep_line,"",reps - 1)
# ^don't replace the last one
但是這種直接的方法將拿起與文本(如"hello <this is repeated>"
例如)結束,並且這可以通過也檢查到有一個\ n中的行之前右邊被固定線:
reps = rest.count("\n"+rep_line)
new_text = beginning + rest.replace("\n"+rep_line,"\n",reps - 1)
#^replace with a single newline
閱讀線然後比較這些行......如果當前行與上一行不同,請將其寫入輸出文件,否則跳過它並寫入空白行......沒有更多內容...... –
會用空行替換它還是刪除整行? –