parse lines to name value pairs

Discussion in 'Python' started by sunriselee, Feb 27, 2005.

  1. sunriselee

    sunriselee Guest

    Hi all,

    I want to parse some string lines into name value pairs, where the value
    will be a list. Here are some sample lines:

    line1 = """path {{data/tom} C:/user/john}"""
    line2 = """books{{book music red} {book {math 1}
    blue} {book {tom's book} green}}"""

    For line1, the name is "path", the name-value should be the following
    forms:

    ["path", ["data/tom", "C:/user/john"]]

    For line 2, the name is "books", and the value should be a list in either
    one of the following forms (either one is ok):

    ["books", [["book","music","red"],["book","math 1","blue"],["book", "tom's
    book", "green"]]]

    or

    ["books", ["book","music","red","book","math 1","blue","book", "tom's
    book", "green"]]

    Any ideas?

    Many thanks!
     
    sunriselee, Feb 27, 2005
    #1
    1. Advertising

  2. sunriselee

    Paul McGuire Guest

    Gee, no takers?

    Here's a (surprise!) pyparsing solution.

    -- Paul


    ..# get pyparsing at http://pyparsing.sourceforge.net
    ..
    ..from pyparsing import quotedString, printables, Word, OneOrMore,
    Forward, Literal,delimitedList,Group
    ..quotedString.setParseAction(lambda s,l,t: t[0].strip("'\""))
    ..
    ..line1 = """path {{data/tom} C:/user/john}"""
    ..line2 = """books{{book music red} {book {math 1} blue} {book {tom's
    book} green}}"""
    ..
    ..lbrace = Literal("{").suppress()
    ..rbrace = Literal("}").suppress()
    ..listDef = Forward()
    ..
    ..nonbracechars = "".join([ c for c in printables if c not in "{}"])
    ..
    ..# add more things to listItem, such as integers, etc. if your list has
    other than quoted strings
    ..listItem = OneOrMore(Word(nonbracechars)) | quotedString | listDef
    ..
    ..print "With grouping"
    ..listDef << lbrace + Group( OneOrMore(listItem) ) + rbrace
    ..lineDef = Word(nonbracechars) + Group(listDef)
    ..
    ..for testdata in (line1, line2):
    .. print lineDef.parseString(testdata).asList()
    ..
    ..print
    ..
    ..print "Without grouping"
    ..listDef << lbrace + ( OneOrMore(listItem) ) + rbrace
    ..lineDef = Word(nonbracechars) + Group(listDef)

    Givest the following output:
    With grouping
    ['path', [[['data/tom'], 'C:/user/john']]]
    ['books', [[['book', 'music', 'red'], ['book', ['math', '1'], 'blue'],
    ['book', ["tom's", 'book'], 'green']]]]

    Without grouping
    ['path', ['data/tom', 'C:/user/john']]
    ['books', ['book', 'music', 'red', 'book', 'math', '1', 'blue', 'book',
    "tom's", 'book', 'green']]


    for testdata in (line1, line2):
    print lineDef.parseString(testdata).asList()
     
    Paul McGuire, Feb 28, 2005
    #2
    1. Advertising

  3. sunriselee

    Paul McGuire Guest

    Damned cut-and-paste in GoogleGroups edit window (leading '.'s are to
    retain spacing):

    ..# get pyparsing at http://pyparsing.sourceforge.net
    ..
    ..from pyparsing import quotedString, printables, Word, OneOrMore,
    Forward, Literal,delimitedList,Group
    ..
    ..line1 = """path {{data/tom} C:/user/john}"""
    ..line2 = """books{{book music red} {book {math 1} blue} {book {tom's
    book} green}}"""
    ..
    ..lbrace = Literal("{").suppress()
    ..rbrace = Literal("}").suppress()
    ..listDef = Forward()
    ..
    ..nonbracechars = "".join([ c for c in printables if c not in "{}"])
    ..
    ..# add more things to listItem, such as integers, etc. if your list has
    other than quoted strings
    ..listItem = OneOrMore(Word(nonbracechars)) | quotedString | listDef
    ..
    ..print "With grouping"
    ..listDef << lbrace + Group( OneOrMore(listItem) ) + rbrace
    ..lineDef = Word(nonbracechars) + Group(listDef)
    ..
    ..for testdata in (line1, line2):
    .. print lineDef.parseString(testdata).asList()
    ..
    ..print
    ..
    ..print "Without grouping"
    ..listDef << lbrace + ( OneOrMore(listItem) ) + rbrace
    ..lineDef = Word(nonbracechars) + Group(listDef)
    ..
    ..for testdata in (line1, line2):
    .. print lineDef.parseString(testdata).asList()
    ..

    Gives this output:

    With grouping
    ['path', [[['data/tom'], 'C:/user/john']]]
    ['books', [[['book', 'music', 'red'], ['book', ['math', '1'], 'blue'],
    ['book', ["tom's", 'book'], 'green']]]]

    Without grouping
    ['path', ['data/tom', 'C:/user/john']]
    ['books', ['book', 'music', 'red', 'book', 'math', '1', 'blue', 'book',
    "tom's", 'book', 'green']]
     
    Paul McGuire, Feb 28, 2005
    #3
    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. I R BABOON
    Replies:
    0
    Views:
    316
    I R BABOON
    Oct 21, 2003
  2. TR
    Replies:
    3
    Views:
    1,232
  3. =?Utf-8?B?RGlmZmlkZW50?=
    Replies:
    3
    Views:
    825
    Juan T. Llibre
    May 13, 2005
  4. Replies:
    1
    Views:
    1,670
    Bjorn Abelli
    Apr 24, 2006
  5. Johan Martinez
    Replies:
    3
    Views:
    293
    Johan Martinez
    May 22, 2011
Loading...

Share This Page