简体   繁体   中英

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.

A regex solution:

import re
f = open('drama.txt', 'r')
content = f.read()
x = re.findall(r'Characters:(.*?)First scene\.', content, re.DOTALL)
print("".join(x))

'''
Peter, the king. 
Anna, court lady. 
Michael, caretaker. 
Andre, soldier. 
Tina, baker.
'''

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM