Dec-15-2017, 06:25 PM
Or, just don't write a line unless the previous line is fine.
text_to_avoid = "bad" with open("original.txt") as infile: with open("out.txt", "w") as outfile: last_line = "" line_before_last = "" for ndx, line in enumerate(infile): if ndx >= 2: if text_to_avoid not in line.lower() \ and text_to_avoid not in last_line.lower() \ and text_to_avoid not in line_before_last.lower(): print(last_line, file=outfile) line_before_last = last_line last_line = line if text_to_avoid not in last_line.lower() \ and text_to_avoid not in line_before_last.lower(): print(last_line, file=outfile)