-2

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)
Mady
  • 443
  • 7
  • 19
  • 2
    What's this supposed to be: `for (line[i-1]) in lines:`??? – Matthieu Brucher Nov 13 '18 at 15:35
  • i wanted to get this line 'This shall be deleted.' with the expression. But it doesn't work – Mady Nov 13 '18 at 15:37
  • What do you want to test? This is not a valid Python for loop. it's suppose to be `for variable in container`, and this is not a variable. – Matthieu Brucher Nov 13 '18 at 15:40
  • Well i want to delet this 'This shall be deleted. Titel' lines. Title is always the same but the line above is always different. Ididn't know how else to express this. Do you maybe have a different solution or approach for me ? – Mady Nov 13 '18 at 15:42

2 Answers2

2

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
kantal
  • 2,331
  • 2
  • 8
  • 15
1

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)
Nathan
  • 3,558
  • 1
  • 18
  • 38
  • This code works very good. Thanks. Can i also adjust itto delet the previous 3 lines, or is this not possible with this ? – Mady Nov 13 '18 at 16:39
  • @Mady I'm sure it would work just fine if you adjust it correctly. Personally I really like kantals solution more so you might want to check that out as well. – Nathan Nov 13 '18 at 17:02