Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parse lines to name value pairs

Reply
Thread Tools

parse lines to name value pairs

 
 
sunriselee
Guest
Posts: n/a
 
      02-27-2005
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!

 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      02-28-2005
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()

 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      02-28-2005
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']]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace lines in a file using hash key-value pairs Johan Martinez Ruby 3 05-22-2011 03:07 AM
How do I get the column name/value pairs from a ResultSet? laredotornado@zipmail.com Java 1 04-24-2006 04:27 PM
Displaying the all the name-value pairs of the session object? =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 3 05-12-2005 11:54 PM
WebRequest : how to submit name-value pairs? TR ASP .Net 3 01-11-2005 01:23 PM
Handling name value pairs in a web service I R BABOON ASP .Net 0 10-21-2003 12:20 AM



Advertisments