如何忽略pyparsing ParseException并继续?

问题描述 投票:1回答:1

我想忽略文件中与所有预定义解析器不匹配的行并继续。我想忽略的行在很宽的范围内,我无法检查并为每个行定义解析器。

捕获ParseException后,我使用try..except和pass。但是,解析会立即停止。

try:
    return parser.parseFile(filename, parse_all)

except ParseException, err:
    msg = 'Error during parsing of {}, line {}'.format(filename, err.lineno)
    msg += '\n' + '-'*70 + '\n'
    msg += err.line + '\n'
    msg += ' '*(err.col-1) + '^\n'
    msg += '-'*70 + '\n' + err.msg
    err.msg = msg

    print(err.msg)
    pass

即使存在ParseException,我也想继续。

python text-processing pyparsing
1个回答
2
投票

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'
© www.soinside.com 2019 - 2024. All rights reserved.