考虑以下最小例子:
from pyparsing import Word, delimitedList
the_list = delimitedList(Word("fine").setResultsName("extension", listAllMatches=True))
prefixed = Word("okay").setResultsName("base") + the_list
prefixed.addParseAction(lambda x: map(lambda element: x.base + element, x.extension))
final = prefixed.setResultsName("doesNotWork", listAllMatches=True) + Word("x")
final.parseString("ookf,i,n,ex")
返回
(['ookf', 'ooki', 'ookn', 'ooke', 'x'], {'doesNotWork': [((['ookf'], {}), 0)]})
如何让pyparsing将整个列表,['ookf','ooki','ookn','ooke','x']分配给doesNotWork,而不仅仅是第一个列表项?
答案 0 :(得分:2)
如果您将prefixed
更改为:
prefixed = Group(Word("okay").setResultsName("base") + the_list)
这可以接受吗?