I am having trouble getting setResultsName to work in this script, even when trying to emulate the above examples. I looked through the documentation, consulted the author’s book and looked at the examples on the forum. I tried several options, and I am frankly a little puzzled, although I am sure there is something stupid that I am doing wrong, because I am not very experienced in this.
from pyparsing import *
lineId = Word(nums)
topicString = Word(alphanums+'-'+' '+"'")
expr = Forward()
full_entry = Group(lineId('responsenumber') + expr)
def new_line():
return '\n' + lineId.responsenumber
expr << topicString + Optional(nestedExpr(content=delimitedList(expr))) + Optional((Literal(';').setParseAction(new_line) + expr))
for line in input:
inputParseResults = delimitedList(full_entry).parseString(line).asList()
print inputParseResults
What this piece of code is trying to do is take this input:
1768 dummy data; things
27483 other things
And let it break the line at the semicolon, append lineId again, and then re-associate it, as you see in this line:
1768 dummy data
1768 things
27483 other things
, ; - + lineId, , setResultsName , .