[Pyparsing] delimitedList Results
Brought to you by:
ptmcg
From: Loïc B. <lo...@li...> - 2014-04-09 17:27:29
|
Hi, I have some questions on how to use properly parseResults associated with a delimitedList. Here is the kind of line I would like to parse : [code] string = 'line 22 width=2;type=1 V101-,V103,V99+,V12 L102' [/code] And here is the parser I've built to extract information from this string: [code] import pyparsing as p integer = p.Word(p.nums) name = p.Word(p.alphas, p.alphanums+'_') ident = p.Combine(name('id') + p.Optional(p.oneOf('- +'))) lineParser = ( 'line' + integer('num') + 'width=' + integer('width')+';' + 'type=' + integer('type') + p.delimitedList(ident)('points') + name('name') ) r = lineParser.parseString(string) [/code] I can access to simple keys : >>> print r.name L102 but with delimitedList, it seems to be a bit more trickier as the associated parseResults can not be used as a list of ParseResults but rather as a list of strings: >>> for pt in r.points: >>> print type(pt), pt <type 'str'> V101- <type 'str'> V103 <type 'str'> V99+ <type 'str'> V12 I can get the list of point directly with r.points.asList(), but I don't know how to access to the list of ids. I was expecting that I could use classical __getitem__ method to list all ids with something like [ pt.id for pt in r.points] Is there any way to do this ? Besides, I don't fully understand the structure of this parseResult : >>> print repr(r.points) (['V101-', 'V103', 'V99+', 'V12'], {'id': [('V101', 0), ('V103', 1), ('V99', 2), ('V12', 3)]}) it seems that the id key is associated with the list that I m looking for, but when I try to retrieve it I only get one element of that list: >>> r.points.id V12 Have you got any clue? Regards -- Loïc |