Python read through file until match, read until next pattern

后端 未结 2 517
暗喜
暗喜 2021-01-20 12:32

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 l

相关标签:
2条回答
  • 2021-01-20 13:16

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

    0 讨论(0)
  • 2021-01-20 13:21

    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:

    def record(filename, pattern):
        with open(filename) as file:
            recording = False
            for 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')))
    
    0 讨论(0)
提交回复
热议问题