This kind of parsing of tabular data is one of the original cases that pyparsing was written for. Congratulations on getting this far with parsing a non-trivial input text!
Rather than try to do any unnatural Grouping or whatnot to twist or combine the parsed data into your desired data structure, I'd just walk the parsed results as you've got them and build up a new summary structure, which I'll call summary
. We are actually going to accumulate data into this dict, which strongly suggests using a defaultdict for simplified initialization of the summary when a new key is found.
from collections import defaultdict
summary = defaultdict(dict)
Looking at the current structure returned in p
, you are getting item headers and detailed data sets gathered into the named results it
and details
. We can zip these together to get each section's headers and data. Then for each line in the details, we'll make a dict of the detailed values by zipping the item headers with the parsed data values. Then we'll update the summary value that is keyed by the line.date
:
for items,details in zip(p.it,p.details):
for line in details:
summary[line.date[0]].update(dict(zip(items,line.data)))
Done! See what the keys are that we have accumulated:
print summary.keys()
gives:
['20.04.2013', '18.04.2013', '17.04.2013', '19.04.2013']
Print the data accumulated for '18.04.2013':
print summary['18.04.2013']
gives:
{1: Decimal('548481.10'), 2: Decimal('46383.00'), 3: Decimal('29810.00'), 4: Decimal('548481.10'), 5: Decimal('56383.00')}