Search code examples
pythonfilepattern-matchingrowdeleting

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


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)

Solution

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