>>> test = "somestring 363 1 46 17,363 1 34 17,401 3 8 14,"
Here is a pyparsing processor for your input string:
>>> from pyparsing import *
>>> integer = Word(nums)
>>> patt = Word(alphas) + OneOrMore(Group(integer*4 + Suppress(',')))
Using patt.parseString returns a pyparsing ParseResults object, which has some nice list/dict/object properties. First, just printing out the results as a list:
>>> patt.parseString(test).asList()
['somestring', ['363', '1', '46', '17'], ['363', '1', '34', '17'], ['401', '3', '8', '14']]
See how each of your groups is grouped as a sublist?
Now let's have the parser do a bit more work for us. At parse time, we already know we are parsing valid integers - anything matching Word(nums)
has to be an integer. So we can add a parse action to do this conversion at parse time:
>>> integer = Word(nums).setParseAction(lambda tokens:int(tokens[0]))
Now, we recreate our pattern, and parsing now gives us groups of numbers:
>>> patt = Word(alphas) + OneOrMore(Group(integer*4 + Suppress(',')))
>>> patt.parseString(test).asList()
['somestring', [363, 1, 46, 17], [363, 1, 34, 17], [401, 3, 8, 14]]
Lastly, we can also assign names to the bits parsed out of this input:
>>> patt = Word(alphas)("desc") + OneOrMore(Group(integer*4 + Suppress(',')))("numgroups")
The list of returned items is the same:
>>> patt.parseString(test).asList()
['somestring', [363, 1, 46, 17], [363, 1, 34, 17], [401, 3, 8, 14]]
But if we dump() the results, we see what we can access by name:
>>> print patt.parseString(test).dump()
['somestring', [363, 1, 46, 17], [363, 1, 34, 17], [401, 3, 8, 14]]
- desc: somestring
- numgroups: [[363, 1, 46, 17], [363, 1, 34, 17], [401, 3, 8, 14]]
We can use those names for dict-like or attribute-like access. I'm partial to the attribute style myself:
>>> res = patt.parseString(test)
>>> print res.desc
somestring
>>> print res.numgroups
[[363, 1, 46, 17], [363, 1, 34, 17], [401, 3, 8, 14]]
>>> for ng in res.numgroups: print sum(ng)
...
427
415
426
Here is the entire parser and output processor:
test = "somestring 363 1 46 17,363 1 34 17,401 3 8 14,"
from pyparsing import *
integer = Word(nums).setParseAction(lambda tokens:int(tokens[0]))
patt = Word(alphas)("desc") + \
OneOrMore(Group(integer*4 + Suppress(',')))("numgroups")
print patt.parseString(test).asList()
print patt.parseString(test).dump()
res = patt.parseString(test)
print res.desc
print res.numgroups
for ng in res.numgroups:
print sum(ng)