我必須從文本文件以及上一行和下一行刪除指定的行。 我可以刪除指定的行,但我不知道如何刪除下一個和上一個。 這裏是我的代碼python從文本文件中刪除下一行和前一行。基於樣本
def Parser(line):
global destination
if "CHECKIN" in line:
splitted = line.split()
destination = open(ea_xml_dest,'r')
lines = destination.readlines()
destination = open(ea_xml_dest,'w')
sample = str(splitted[5][:-1])
for i in lines:
if sample not in i:
destination.write(i)
destination.close()
destination = open(ea_xml_dest,'r')
這個腳本不動什麼包含樣品到新文件 如何避免以前和下一行的行?
下一個元素髮現後,將不被刪除 lines = lines [:found-1] + lines [found + 2 :] – phoenix
@phoenix:謝謝你,鳳凰! –