[Pyparsing] Reading n words
Brought to you by:
ptmcg
From: Andreas M. <and...@gm...> - 2008-09-03 21:24:55
|
Hello, I'm trying to parse a string like '3 foo bar baz', where the number in the string specifies the number of the following words. Is there a more elegant way to achieve this than with the following code? Ciao Andreas from pyparsing import * def foo(st): p1 = Word(nums) n = p1.parseString(st)[0] p2 = Literal(n) + Word(alphas)*int(n) print p2.parseString(st) foo('1 foo') foo('2 foo baz') foo('3 foo bar baz') |