Repeatedly extract a line between two delimiters in a text file, Python

后端 未结 4 708
情话喂你
情话喂你 2020-12-06 11:07

I have a text file in the following format:

DELIMITER1
extract me
extract me
extract me
DELIMITER2

I\'d like to extract every block of

4条回答
  •  感动是毒
    2020-12-06 11:35

    You can simplify this to one regular expression using re.S, the DOTALL flag.

    import re
    def GetTheSentences(infile):
         with open(infile) as fp:
             for result in re.findall('DELIMITER1(.*?)DELIMITER2', fp.read(), re.S):
                 print result
    # extract me
    # extract me
    # extract me
    

    This also makes use of the non-greedy operator .*?, so multiple non-overlapping blocks of DELIMITER1-DELIMITER2 pairs will all be found.

提交回复
热议问题