diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-07-19 23:33:47 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-07-19 23:33:47 -0500 |
commit | 33ca34cc24282f39a1a9185bb713a6cafd38fa58 (patch) | |
tree | 97731af0ae5452a40d6ec682c62b19295c225702 | |
parent | de00f571f40a7e6478e7446ec1bcdb7472af32ee (diff) | |
download | pyparsing-git-33ca34cc24282f39a1a9185bb713a6cafd38fa58.tar.gz |
Change example to use addCondition instead of parse action that raises ParseException
-rw-r--r-- | examples/removeLineBreaks.py | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/examples/removeLineBreaks.py b/examples/removeLineBreaks.py index 84bd33e..90b77e4 100644 --- a/examples/removeLineBreaks.py +++ b/examples/removeLineBreaks.py @@ -11,21 +11,20 @@ # # Copyright 2006, by Paul McGuire # -from pyparsing import * +import pyparsing as pp # define an expression for the body of a line of text - use a parse action to reject any # empty lines -def mustBeNonBlank(s,l,t): - if not t[0]: - raise ParseException(s,l,"line body can't be empty") -lineBody = SkipTo(lineEnd).setParseAction(mustBeNonBlank) +def mustBeNonBlank(s, l, t): + return bool(t[0]) + +lineBody = pp.SkipTo(pp.lineEnd).addCondition(mustBeNonBlank, message="line body can't be empty") # now define a line with a trailing lineEnd, to be replaced with a space character -textLine = lineBody + Suppress(lineEnd).setParseAction(replaceWith(" ")) +textLine = lineBody + pp.Suppress(pp.lineEnd).setParseAction(pp.replaceWith(" ")) # define a paragraph, with a separating lineEnd, to be replaced with a double newline -para = OneOrMore(textLine) + Suppress(lineEnd).setParseAction(replaceWith("\n\n")) - +para = pp.OneOrMore(textLine) + pp.Suppress(pp.lineEnd).setParseAction(pp.replaceWith("\n\n")) # run a test test = """ @@ -41,5 +40,6 @@ test = """ print(para.transformString(test)) # process an entire file -z = para.transformString(open("Successful Methods of Public Speaking.txt").read()) -open("Successful Methods of Public Speaking(2).txt","w").write(z) +original = open("Successful Methods of Public Speaking.txt").read() +transformed = para.transformString(original) +open("Successful Methods of Public Speaking(2).txt", "w").write(transformed) |