regular expression or parser ?

  • Thread starter Forced_Ambitions
  • Start date
F

Forced_Ambitions

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.
 
K

Kent Johnson

Forced_Ambitions said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top