FAQ
I have a file which contains data in the format shown in the sample
bellow.
How can I parse it to get following:
(14,trigger,guard,do_action,15)

Thanks a lot for your postings
Petr Jakes

type:
4
bgrColor:
255 255 255
fgrColor:
0 0 0
objId:
16
Num.Pts:
2
177 104
350 134
objStartId:
14
objEndId:
15
eventName:
trigger
eventCond:
guard
eventAction:
do_action

Search Discussions

  • Petr Jakes at Feb 1, 2006 at 12:32 am
    Sorry I was not precise enough in my description.

    In the file, there is of course more sections starting with the keyword


    type:

    I would like to only analyze sections which start with the sequence:

    type:
    4

    Petr jakes
  • Paul McGuire at Feb 1, 2006 at 1:09 am
    A problem fit for pyparsing! Download pyparsing at
    http://pyparsing.sourceforge.net.

    Assuming you always have these fields, in this order, this program will
    figure them out. If not, you'll need to tweak the pyparsing
    definitions as needed.

    -- Paul


    data = """type:
    4
    bgrColor:
    255 255 255
    fgrColor:
    0 0 0
    objId:
    16
    Num.Pts:
    2
    177 104
    350 134
    objStartId:
    14
    objEndId:
    15
    eventName:
    trigger
    eventCond:
    guard
    eventAction:
    do_action
    """

    from pyparsing import *

    # define literals for field labels
    type_ = Literal("type")
    bgrColor = Literal("bgrColor")
    fgrColor = Literal("fgrColor")
    objId = Literal("objId")
    numPts = Literal("Num.Pts")
    objStartId = Literal("objStartId")
    objEndId = Literal("objEndId")
    eventName = Literal("eventName")
    eventCond = Literal("eventCond")
    eventAction = Literal("eventAction")

    # define an integer, and tell parser to convert them to ints
    intvalue = Word(nums).setParseAction( lambda s,l,toks: int(toks[0]) )

    # define an alphabetic identifier
    alphavalue = Word(alphas,alphanums+"_")

    # define a 2D coordinate, with results names for fields
    coordvalue = Group( intvalue.setResultsName("X") +
    intvalue.setResultsName("Y") )

    # define an RGB color value, with results names for fields
    colorvalue = Group( intvalue.setResultsName("R") +
    intvalue.setResultsName("G") +
    intvalue.setResultsName("B") )

    # compose an entry definition, using above-defined expressions, with
    results names for fields
    entry = ( type_ + ":" + "4" +
    bgrColor + ":" + colorvalue.setResultsName("bgrColor") +
    fgrColor + ":" + colorvalue.setResultsName("fgrColor") +
    objId + ":" + intvalue.setResultsName("objId") +
    numPts + ":" + Group( intvalue.setResultsName("numpts") +
    OneOrMore( coordvalue ).setResultsName("coords")
    ).setResultsName("pts") +
    objStartId + ":" + intvalue.setResultsName("objStartId") +
    objEndId + ":" + intvalue.setResultsName("objEndId") +
    eventName + ":" + alphavalue.setResultsName("eventName") +
    eventCond + ":" + alphavalue.setResultsName("eventCond") +
    eventAction + ":" + alphavalue.setResultsName("eventAction")
    )

    # scan through input data, and retrieve data fields as desired
    for entryData,start,end in entry.scanString(data):
    print
    "(%(objStartId)d,%(eventName)s,%(eventCond)s,%(eventAction)s,%(objEndId)d)"
    % entryData
    print entryData.objId
    print entryData.bgrColor
    print entryData.fgrColor
    print [ (pt.X,pt.Y) for pt in entryData.pts.coords ]
    print [ tuple(pt) for pt in entryData.pts.coords ]


    Prints:
    (14,trigger,guard,do_action,15)
    16
    [255, 255, 255]
    [0, 0, 0]
    [(177, 104), (350, 134)]
    [(177, 104), (350, 134)]
  • John McMonagle at Feb 1, 2006 at 1:22 am

    I have a file which contains data in the format shown in the sample
    bellow.
    How can I parse it to get following:
    (14,trigger,guard,do_action,15)

    Thanks a lot for your postings
    Petr Jakes

    type:
    4
    bgrColor:
    255 255 255
    fgrColor:
    0 0 0
    objId:
    16
    Num.Pts:
    2
    177 104
    350 134
    objStartId:
    14
    objEndId:
    15
    eventName:
    trigger
    eventCond:
    guard
    eventAction:
    do_action


    Create a dictionary mapping the properties to their values, then print
    out the ones you require:

    fid = open('xxx', 'r')
    lines = fid.readlines()
    fid.close()

    i = 0
    items = {}
    for line in lines:
    line = line.strip()
    if line[-1] == ':':
    line = line.split(':')[0]
    items[line] = lines[i+1].strip()
    i = i + 1
    outString = '(' + items.get('objStartId') + ',' +
    items.get('eventName') + ',' + items.get('eventCond') + ',' +
    items.get('eventAction') + ',' + items.get('objEndId') + ')\n'

    print outString



    This code yields the following output:

    (14,trigger,guard,do_action,15)

    Regards,

    John McMonagle



    --
    This message has been scanned for viruses and
    dangerous content by MailScanner, and is
    believed to be clean.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 1, '06 at 12:22a
activeFeb 1, '06 at 1:22a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase