1 回答
TA贡献1776条经验 获得超12个赞
Pyparsing 并没有真正的“出错时继续”选项,因此您需要调整解析器,使其不会首先引发 ParseException。您可能会尝试在解析器中添加类似| SkipTo(LineEnd())('errors*')最后一招的东西。然后,您可以查看错误结果名称以查看哪些行误入歧途(或向该表达式添加解析操作以捕获更多内容,而不仅仅是当前行)。
import pyparsing as pp
era = "The" + pp.oneOf("Age Years") + "of" + pp.Word(pp.alphas)
era.runTests("""
The Age of Enlightenment
The Years of Darkness
The Spanish Inquisition
""")
印刷:
The Age of Enlightenment
['The', 'Age', 'of', 'Enlightenment']
The Years of Darkness
['The', 'Years', 'of', 'Darkness']
The Spanish Inquisition
^
FAIL: Expected Age | Years (at char 4), (line:1, col:5)
添加这些行并再次调用 runTests:
# added to handle lines that don't match
unexpected = pp.SkipTo(pp.LineEnd(), include=True)("no_one_expects")
era = era | unexpected
印刷:
The Age of Enlightenment
['The', 'Age', 'of', 'Enlightenment']
The Years of Darkness
['The', 'Years', 'of', 'Darkness']
The Spanish Inquisition
['The Spanish Inquisition']
- no_one_expects: 'The Spanish Inquisition'
添加回答
举报