How to grep lines between two patterns in a big file with python

前端 未结 5 1983
青春惊慌失措
青春惊慌失措 2021-01-03 12:45

I have a very big file, like this:

[PATTERN1]
line1
line2
line3 
...
...
[END PATTERN]
[PATTERN2]
line1 
line2
...
...
[END PATTERN]

I need to extract

5条回答
  •  旧时难觅i
    2021-01-03 13:13

    Consider:

    # hi
    # there
    # begin
    # need
    # this
    # stuff
    # end
    # skip
    # this
    
    with open(__file__) as fp:
        for line in iter(fp.readline, '# begin\n'):
            pass
        for line in iter(fp.readline, '# end\n'):
            print line
    

    prints "need this stuff"

    More flexible (e.g. to allow re pattern matching) is to use itertools drop- and takewhile:

    with open(__file__) as fp:
        result = list(itertools.takewhile(lambda x: 'end' not in x, 
            itertools.dropwhile(lambda x: 'begin' not in x, fp)))
    

提交回复
热议问题