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

后端 未结 4 706
情话喂你
情话喂你 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:25

    This should do what you want:

    import re
    def GetTheSentences(file):
        start_rx = re.compile('DELIMITER')
        end_rx = re.compile('DELIMITER2')
    
        start = False
        output = []
        with open(file, 'rb') as datafile:
             for line in datafile.readlines():
                 if re.match(start_rx, line):
                     start = True
                 elif re.match(end_rx, line):
                     start = False
                 if start:
                      output.append(line)
        return output
    

    Your previous version looks like it's supposed to be an iterator function. Do you want your output returned one item at a time? That's slightly different.

提交回复
热议问题