Search code examples
python-3.xlines

How can I start reading text at a specific line and stop and specific line


Long time listener first time caller, I'm quite new to this so please be kind.

I have a large text document and I would like to strip out the headers and footers. I would like to trigger the start and stop reading lines with specific strings in the text.

filename ='Bigtextdoc.txt'
startlookup = 'Foo'
endlookup = 'Bar'
with open(filename, 'r') as infile: 
    for startnum, line in enumerate(infile, 1):
        if startlookup in line:
            data = infile.readlines()
            for endnum, line in enumerate(infile, 1):
                if endlookup in line:
                    break   
print(data)

like this I can read the lines after the header contain 'Foo' and If I move the data = line after the if endlookup line it will only read the line in the footer starting at 'Bar'

I don't know how to start at Foo and stop at Bar?


Solution

  • For readability I'll extract the logic in a function like:

    def lookup_between_tags(lines, starttag, endtag):
        should_yield = False
        for line in lines:
            if starttag in line:
                should_yield = True
            elif endtag in line:
                should_yield = False
            if should_yield:
                yield line
    

    Using the fact that an opened file is iterable, it can be used like:

    with open('Bigtextdoc.txt') as bigtextdoc:
        for line in lookup_between_tags(bigtextdoc, 'Foo', 'Bar'):
            print(line)