Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Read a block in a large text file in python ?

By M.K. Verma · 3 May 2022

Read a block in a large text file in python ?

You compare lines with "\n" at the ends. You also continue, when you find the start marker.

beginOutput = false
with open("test.txt") as ff:
    for line in ff:
        if line.startswith('.Connect U5'):
            beginOutput = true

        if not beginOutput:
            continue

        if line.strip() == ".EndC":
            break

        print(line)

Note the strip(). It's not the best solution, because it will strip all the lines in your file. Try to strip() only the lines which start with .EndC marker