Loïc Berthe
2014-04-09 17:07:37 UTC
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]
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
<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 ?
2), ('V12', 3)]})
it seems that the id key is associated with the list that I m looking for, but
Have you got any clue?
Regards
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]
print r.name
L102but 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
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 ?
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
r.points.id
V12Have you got any clue?
Regards
--
Loïc
Loïc