Pyparsing question

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

    Pyparsing question

    Hi,

    I am a newbie to Python and pyparsing. I am having difficulty creating a
    grammar that spans multiple lines, the input data look like this

    RTSP/1.0 200 OK\r\n
    Cseq: 1\r\n
    Session: 12345-1\r\n
    \r\n

    Greatly appreciate if anyone can give me a sample or point me to the
    right direction

    Thanks,


    Khoa Nguyen


  • Paul McGuire

    #2
    Re: Pyparsing question

    "Khoa Nguyen" <KNguyen@MEGIST O.com> wrote in message
    news:mailman.13 .1084809534.694 9.python-list@python.org ...[color=blue]
    > Hi,
    >
    > I am a newbie to Python and pyparsing. I am having difficulty creating a
    > grammar that spans multiple lines, the input data look like this
    >
    > RTSP/1.0 200 OK\r\n
    > Cseq: 1\r\n
    > Session: 12345-1\r\n
    > \r\n
    >
    > Greatly appreciate if anyone can give me a sample or point me to the
    > right direction
    >
    > Thanks,
    >
    >
    > Khoa Nguyen[/color]
    =============== ==========
    Khoa -

    In general, pyparsing will ignore line breaks as whitespace. This data
    could be parsed with as broad a grammar as:

    response = Literal("RTSP") + restOfLine.setR esultsName("RTS Pline") + \
    Literal("Cseq:" ) + restOfLine.setR esultsName("Cse qLine") + \
    Literal("Sessio n:") + restOfLine.setR esultsName("Ses sionLine")

    For more structure, enclose each line inside Group objects.
    response = Group( Literal("RTSP") + restOfLine ).setResultsNam e("RTSPline")
    + \
    Group( Literal("Cseq:" ) + restOfLine ).setResultsNam e("CseqLine") + \
    Group( Literal("Sessio n:") + restOfLine ).setResultsNam e("SessionLine" )

    Here is a working (and tested!) sample.

    -- Paul


    =============== ===========

    from pyparsing import Literal, restOfLine, Group

    data = """RTSP/1.0 200 OK
    Cseq: 1
    Session: 12345-1

    """

    print "========"
    print data
    print "========"

    response = Literal("RTSP") + restOfLine.setR esultsName("RTS Pline") + \
    Literal("Cseq:" ) + restOfLine.setR esultsName("Cse qLine") + \
    Literal("Sessio n:") + restOfLine.setR esultsName("Ses sionLine")

    respFields = response.parseS tring(data)
    print respFields
    for k in respFields.keys ():
    print k,"=",respField s[k]

    =============== ============
    Output:

    ========
    RTSP/1.0 200 OK
    Cseq: 1
    Session: 12345-1


    ========
    ['RTSP', '/1.0 200 OK', 'Cseq:', ' 1', 'Session:', ' 12345-1']
    SessionLine = 12345-1
    RTSPline = /1.0 200 OK
    CseqLine = 1
    =============== =============
    Output (using Group'ed constructs):
    ========
    RTSP/1.0 200 OK
    Cseq: 1
    Session: 12345-1


    ========
    [['RTSP', '/1.0 200 OK'], ['Cseq:', ' 1'], ['Session:', ' 12345-1']]
    SessionLine = ['Session:', ' 12345-1']
    RTSPline = ['RTSP', '/1.0 200 OK']
    CseqLine = ['Cseq:', ' 1']


    Comment

    Working...