Python - deleting lines and previos lines (matching pattern patterns)

865 Views Asked by At

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)
2

There are 2 best solutions below

2
On BEST ANSWER

First keep track of which lines you want to copy:

lines_to_keep = []
with open(file1) as f_in:
    deleted_previous_line = True
    for line in f_in:
         if line.startswith(('Text', 'Titel')):
              if not deleted_previous_line:
                    del lines_to_keep[-1]
              deleted_previous_line = True
              continue
         deleted_previous_line = False
         lines_to_keep.append(line)

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

with open(file2, 'w') as f_out:
    f_out.writelines(lines_to_keep)
0
On

You don't have to read the file at once. Read the lines after each other, and store the current line, but write it out only after the next read, or not.

with open("file1") as finp, open("file2","w") as fout:

         lprev=""
         for line in finp:

             if line.startswith("Titel") or line.startswith("Text"):
                 lprev=""
                 continue
             if lprev:
                 fout.write(lprev)
             lprev=line

         if lprev:
             fout.write(lprev)  # write out the last line if needed