I want to find the lines which start with a word of a list. If the word is found i want the line it stands in and the previous line to be deleted. I am able to get the line and the previos one and print them but i can not get my head around not to pass them to my outputfile. F.e.:
in-put:
This is not supposed to be deleted.
This shall be deleted.
Titel
This is not supposed to be deleted.
This is not supposed to be deleted
out-put:
This is not supposed to be deleted.
This is not supposed to be deleted.
This is not supposed to be deleted
I tried it with this code, but i keep getting a TypeError: 'str' object does not support item assignment
with open(file1) as f_in, open(file2, 'w') as f_out:
lines = f_in.read().splitlines()
for i, line in enumerate(lines):
clean = True
if line.startswith(('Text', 'Titel')):
for (line[i-1]) in lines:
clean = False
for line in lines:
clean =False
if clean == True:
f_out.write(line)
First keep track of which lines you want to copy:
The trick with the
deleted_previous_line
is necessary to ensure it does not delete too many lines if consecutive lines start with 'Text' or 'Titel'.Then write it to your output file