I have the following file:
This
is
a
testfile
wj5j keyword 1
WFEWF
O%LWJZ keyword 2
which
should
lpokpij keyword 3
123123das
kpmnvf keyword 4
just
contain
the
following
lines.
from which I need to delete the subsets of lines between keyword 1 & keyword 2 as well as between keyword 3 & keyword 4, hence it would looks like that:
This
is
a
testfile
which
should
just
contain
the
following
lines.
I tried the following which prints only the lines of code containing the keywords, but not those lines in between. My idea was if I got all the lines printed, I could delete them from the file
with open ("newfile_TEST1.txt", mode = "r") as file:
keywords = ['keyword 1', 'keyword 2','keyword 3','keyword 4']
lines = file.readlines()
for lineno, line in enumerate(file,1):
matches = [k for k in keywords if k in line]
if matches:
print(line)
What can I do to improve my code?