Search code examples
pythonpython-3.xprintingtext-fileslines

Print multiple lines between two specific lines (keywords) from a text file


I have a textfile and want to print the lines between two other lines, using Python 3.5 on Windows. I want to print the characters of a drama to another file. The textfile looks like this:

...
Characters:
Peter, the king.
Anna, court lady.
Michael, caretaker.
Andre, soldier.
Tina, baker.
First scene.
...

I want to print all the character names between the lines "Characters:" and "First scene." My first try was:

newfile = open('newfile.txt', 'w')
with open('drama.txt', 'r') as f:
for line in f:
    if line.startswith('Characters:'):
        print(next(f), file = newfile)

But this only prints one line and i need several lines and the iteration with the next() function led always to a StopIteration Error after printing one line. So is there a way to say: Print all lines between the lines "Characters:" and "First Scene."? It is not really possible to work with indices, because i'm doing it for several dramas and they have all a different number of characters.


Solution

  • You can set a boolean to know if to print a line or not:

    newfile = open('newfile.txt', 'w')
    
    printing = False
    
    with open('drama.txt', 'r') as f:
        for line in f:
            if line.startswith('Characters:'):
                printing = True
                continue # go to next line
            elif line.startswith('First scene'):
                printing = False
                break # quit file reading
    
            if printing:
                print(line, file=newfile)
    newfile.close()