CarolL
CarolL

Reputation: 81

How to ignore pyparsing ParseException and proceed?

I would like to ignore the line in a file which does not match with all predefined parser and proceed. The line(s) that I would like to ignore is in wide range which I couldn't inspect and define parser for each of them.

I use try..except with pass once ParseException is caught. However, the parsing stops immediately.

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

I would like to proceed even if there is a ParseException.

Upvotes: 2

Views: 1413

Answers (1)

PaulMcG
PaulMcG

Reputation: 63709

Pyparsing doesn't really have a "continue on error" option, so you'll need to adjust your parser so that it doesn't raise the ParseException in the first place. What you might try is adding to your parser something like | SkipTo(LineEnd())('errors*') as a last-ditch catch-all. Then you could look at the errors results name to see what lines went astray (or add a parse action to that expression to capture more than just the current line).

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
    """)

Prints:

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)

Add these lines and call runTests again:

# added to handle lines that don't match
unexpected = pp.SkipTo(pp.LineEnd(), include=True)("no_one_expects")
era = era | unexpected

Prints:

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'

Upvotes: 2

Related Questions