regular expression or parser ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Forced_Ambitions

    #1

    regular expression or parser ?

    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.

  • Kent Johnson

    #2
    Re: regular expression or parser ?

    Forced_Ambition s wrote:[color=blue]
    > 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.
    >[/color]

    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

    Comment

    Working...