<p>要直接回答您的问题,请用<code>originalTextFor</code>包装您的值定义,这将返回匹配标记来自的字符串片段,作为单个字符串。您还可以添加一个解析操作,例如:</p>
<pre><code>value.setParseAction(lambda t : ' '.join(t))
</code></pre>
<p>但这将显式地在每个项之间放置一个空格,而此时可能没有空格(在单词后面有“,”)或多个空格。<code>originalTextFor</code>将给出精确的输入子字符串。但更简单的是,如果您只是阅读“:”之后的所有内容,则可以使用<code>restOfLine</code>。(当然,最简单的方法是使用<code>split(':')</code>,但我假设您是在专门询问如何使用pyparsing来实现这一点)</p>
<p>其他几点注意事项:</p>
<ul>
<li><p><code>xxx.setResultsName('yyy')</code>可以缩短为<code>xxx('yyy')</code>,从而提高解析器定义的可读性。</p></li>
<li><p>将值定义为<code>OneOrMore(Word(unicode_printables) | Literal(','))</code>有几个问题。首先,','将包含在<code>unicode_printables</code>中的字符集中,因此''将与任何已解析的单词一起包含在中。解决这个问题的最好方法是使用<code>excludeChars</code>参数来<code>Word</code>,这样你的句子单词就不会包含逗号:<code>OneOrMore(Word(unicode_printables, excludeChars=',') | ',')</code>。现在您还可以排除其他可能的标点符号,如“;”、“-”等,只需将它们添加到excludeChars字符串中即可。(我刚刚注意到,您正在使用“.”作为<code>delimitedList</code>的分隔符-要使这一点起作用,您还必须将“.”包含在排除的字符中。)Pyparsing在这方面不像正则表达式-如果下一个字符继续匹配当前标记,它不会提前尝试匹配解析器中的下一个标记。这就是为什么你必须自己做一些额外的工作来避免阅读过多。一般来说,像<code>OneOrMore(Word(unicode_printables))</code>这样的开放式的东西很可能会耗尽输入字符串的其余部分。</p></li>
</ul>