regular expression or parser ?
Kent Johnson
kent at kentsjohnson.com
Tue Jan 3 08:21:59 EST 2006
Forced_Ambitions wrote:
> Hi,
>
> 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
like this:
f= open('data.txt')
try:
while True:
# Skip to the next start
while f.next().strip() != 'start operation':
continue
# process lines
while True:
line = f.next().strip()
if line == 'stop operation':
break
# process line
except StopIteration:
pass
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.
Kent
More information about the Python-list
mailing list