regular expression or parser ?
kent at kentsjohnson.com
Tue Jan 3 14:21:59 CET 2006
> I m a novice to python..I m stuck in a problem and need some help.
> i m trying to extract data between the line "start operation" and the
> line "stop operation"
> from a txt file. and then to fill it under different columns of an
> excel sheet.
A simple stateful loop may be enough for the parsing part. Something
# Skip to the next start
while f.next().strip() != 'start operation':
# process lines
line = f.next().strip()
if line == 'stop operation':
# process line
If you only have one block to process and you are confident it will
always be present then the try / except and outer while loop are not needed.
If you can live with CSV output instead of XLS then see the csv module
for the processing part.
More information about the Python-list