Re: [Pyparsing] Grouping when using asList()
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2008-03-06 03:26:46
|
Joshua - <lightbulb>Ah, now I see why you are confused!</lightbulb> The Group class does *not* do grouping like you see in SQL GROUP BY or the itertools groupby. Group is a way for a grammar developer to impart structure to the parsed tokens. The default behavior is for all the tokens to be returned in one flat list. Here is an example: testData = "123 abc 234 def 789 456 xyz" entry = Word(nums) + Optional(Word(alphas)) grammar = OneOrMore(entry) print grammar.parseString(testData).dump() Prints: ['123', 'abc', '234', 'def', '789', '456', 'xyz'] Now we will use Group to "group" the tokens for each entry: entry = Group( Word(nums) + Optional(Word(alphas)) ) Prints: [['123', 'abc'], ['234', 'def'], ['789'], ['456', 'xyz']] You are looking for something a little different. Pyparsing has a feature to implement it, but I've not seen it get much use. It is a qualifier on setResultsName. Before I describe the qualifier itself, let me again show you the default behavior. entry = Word(nums).setResultsName("int") + Optional(Word(alphas).setResultsName("word")) prints: ['123', 'abc', '234', 'def', '789', '456', 'xyz'] - int: 456 - word: xyz The last matching string is the one that is saved for the given results name. But sometimes, you want to keep *all* the matching strings. This is done using the listAllMatches qualifier: entry = Word(nums).setResultsName("int",listAllMatches=True) + Optional(Word(alphas).setResultsName("word",listAllMatches=True)) prints: ['123', 'abc', '234', 'def', '789', '456', 'xyz'] - int: ['123', '234', '789', '456'] - word: ['abc', 'def', 'xyz'] That's it for now, *this* is a long e-mail! :) I'll post the mods to your code in the next e-mail. -- Paul |