Re: [Pyparsing] Access nested ParseResults
Brought to you by:
ptmcg
From: thomas_h <th...@gm...> - 2012-08-18 18:39:14
|
Paul, great, thanks. I was thinking briefly of the Group class during my research, but looking at the documentation thought it might serve a different purpose. I will re-check the material on Group. Cheers, Thomas On Thu, Aug 16, 2012 at 8:28 PM, <pt...@au...> wrote: > > To keep the different names of each pair separate, you'll need to enclose pp1 in a Group: > > pp1=py.Group(py.Word(py.alphanums)("name") + py.Word(py.nums)("number")) > > > Now when you parse your string, r2.list will contain a list of paired name-values: > > r2=pp2.parseString("BAR 10, ZAP 20") > > for pair in r2.list: > print pair.name, pair.number > > To get at an individual element, just using sliced indexing, just like it was a normal list: > > print r2.list[0].name > > -- Paul > > |