regular expression or parser ?

Discussion in 'Python' started by Forced_Ambitions, Jan 3, 2006.

  1. 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.
     
    Forced_Ambitions, Jan 3, 2006
    #1
    1. Advertising

  2. Forced_Ambitions

    Kent Johnson Guest

    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
     
    Kent Johnson, Jan 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,400
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    888
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    12,053
    vforvikash
    Dec 29, 2008
  4. christopher diggins

    YARD : Generic regular expression parser

    christopher diggins, Dec 10, 2004, in forum: C++
    Replies:
    3
    Views:
    451
    Markus Elfring
    Jan 9, 2005
  5. Andrew Warkentin
    Replies:
    3
    Views:
    519
    Paul McGuire
    Mar 3, 2008
Loading...

Share This Page