On 14/05/07, Alan <[EMAIL PROTECTED]> wrote: > I'm looking for a more elegant way to parse sections of text files that > are bordered by BEGIN/END delimiting phrases, like this: > > some text > some more text > BEGIN_INTERESTING_BIT > someline1 > someline2 > someline3 > END_INTERESTING_BIT > more text > more text
If the structure is pretty simple, you could use a state machine approach. eg: import sys infile = open(sys.argv[1], 'r') INTERESTING, BORING = 'interesting', 'boring' interestingLines = [] for line in infile: if line == 'BEGIN_INTERESTING_BIT': state = INTERESTING elif line == 'END_INTERESTING_BIT': state = BORING elif state == INTERESTING: interestingLines.append(line) return interestingLines If you want to put each group of interesting lines into its own section, you could do a bit of extra work (append a new empty list to interestingLines on 'BEGIN', then append to the list at position -1 on state==INTERESTING). HTH! -- John. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor