Hello Everyone!
I need some help to make a code,
I want to make a code which can find a certain string, then delete the line containing it and the line above and below it.
I could make a code to delete the line with the string but how to delete the line above and below also?
For Example
Good Boy
Bad Boy
Sad Boy
Happy Boy
So the code needs to find string "bad" and then delete that line and the line above and below, leaving Only-
Happy Boy
My code only deletes the line containing the string, i wanna code to delete the line above and below as well.
Thanks in advance
I need some help to make a code,
I want to make a code which can find a certain string, then delete the line containing it and the line above and below it.
I could make a code to delete the line with the string but how to delete the line above and below also?
For Example
Good Boy
Bad Boy
Sad Boy
Happy Boy
So the code needs to find string "bad" and then delete that line and the line above and below, leaving Only-
Happy Boy
My code only deletes the line containing the string, i wanna code to delete the line above and below as well.
Thanks in advance
bad_words = ['bad'] with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile: for line in oldfile: if not any(bad_word in line for bad_word in bad_words): newfile.write(line)