You can add results names in the body of a parse action, and they will remain with the parsed tokens.
def addDateTimeResults(tokens):
tokens['date'] = ('20'+tokens.year, tokens.month, tokens.day)
tokens['time'] = (tokens.hour, tokens.min, tokens.sec)
tokens['datetime'] = ParseResults([tokens.date, tokens.time])
for name in ('date', 'time'):
tokens['datetime'][name] = tokens[name]
grammar.setParseAction(addDateTimeResults)
Now in your sample code, add a call to dump()
to see what you get:
parseResults = grammar.parseString("123,084040,ABC,A,231108")
datetime = parseResults.datetime
print datetime.dump()
And you get:
[('2008', '11', '23'), ('08', '40', '40')]
- date: ('2008', '11', '23')
- time: ('08', '40', '40')
Or instead of inserting and returning tuples, you can construct an actual Python datetime object, and return that instead:
import datetime
def addDateTimeResults(tokens):
dtfields = map(int, (tokens[fld] for fld in "year month day hour min sec".split()))
# adjust 2-digit year for 21st century
dtfields[0] += 2000
tokens['datetime'] = datetime.datetime(*dtfields)
Now print parseResults.datetime
gives:
2008-11-23 08:40:40
which is the default string representation of a Python datetime object.