Skip to content Skip to sidebar Skip to footer

Python Read Through File Until Match, Read Until Next Pattern

Python 2.4.3 I need to read through some files (can be as large as 10GB). What I need it to do is go through the file until it matches a pattern. Then print that line and every l

Solution 1:

I don't see any need to use regex here. Not that they're that bad, but if you're looking for such a specific pattern, it's just overkill to use regex. Try something like this:

defrecord(filename, pattern):
    withopen(filename) as file:
        recording = Falsefor line in file:
            if line.startswith(pattern):
                yield line
                recording = not recording
            elif recording:
                yield line

Calling record with a filename and your pattern gives you a generator object yielding line after line. This is better when dealing with large files, so you don't have to slurp them in at once.

Printing your lines could then work like this - assuming your file is named example.txt:

for rec in record(filename, '---- Alpha ---- Zeta'):
    print rec

To be precise: The record generator yields the lines including the line breaks, so you might want to join them back together without any additional line breaks:

print"".join(list(record(filename, '---- Alpha ---- Zeta')))

Solution 2:

You're still matching against line, which doesn't change because you're still in the same iteration of the for loop.

Post a Comment for "Python Read Through File Until Match, Read Until Next Pattern"