Re: [Pyparsing] setResultsName: request for clarification
Brought to you by:
ptmcg
From: Ralph C. <ra...@in...> - 2008-05-01 16:09:53
|
Hi Stefaan, > I have difficulties in understanding how to use setResultsName > correctly. The problem is illustrated in the code below: In the first > version, r.BODY is empty. In the second version, it contains what I > expected it to contain. But I am not sure what the difference is > between these two versions? > > # VERSION 1 > section_body = p.Combine(p.nestedExpr("{","}", > ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) > section_body.setParseAction(p.keepOriginalText) > section_body.setResultsName("BODY") > grammar = p.CaselessKeyword("SECTION").suppress() + section_body > r = grammar.parseString(s) > print r.BODY > > # VERSION 2 > section_body = p.Combine(p.nestedExpr("{","}", > ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) > section_body.setParseAction(p.keepOriginalText) > grammar = p.CaselessKeyword("SECTION").suppress() + \ > section_body.setResultsName("BODY") > r = grammar.parseString(s) > print r.BODY I would guess the intention is you can do address = ... command = address.setResultsName('from') + \ address.setResultsName('to') So your `version 1' is having little effect. Cheers, Ralph. |