diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-06 23:44:02 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-06 23:44:02 -0500 |
commit | a2439508ba5c94546db98593cfa676de9b59babe (patch) | |
tree | 80b02178820811c09b4befc9a9b5efb092813466 /examples/withAttribute.py | |
parent | 832986ffccac943b363da43795c335eafc31b5da (diff) | |
download | pyparsing-git-a2439508ba5c94546db98593cfa676de9b59babe.tar.gz |
Fixed dict structure in makeHTMLTags expressions, and added tag_body attribute to the generated start expression giving easy access to a SkipTo(closeTag) that will parse the tag's body text; some code cleanup and removed duplication among examples
Diffstat (limited to 'examples/withAttribute.py')
-rw-r--r-- | examples/withAttribute.py | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/examples/withAttribute.py b/examples/withAttribute.py index 7fa2bc8..fc0c64a 100644 --- a/examples/withAttribute.py +++ b/examples/withAttribute.py @@ -1,24 +1,26 @@ -#
-# withAttribute.py
-# Copyright, 2007 - Paul McGuire
-#
-# Simple example of using withAttribute parse action helper
-# to define
-#
-data = """\
- <td align=right width=80><font size=2 face="New Times Roman,Times,Serif"> 49.950 </font></td>
- <td align=left width=80><font size=2 face="New Times Roman,Times,Serif"> 50.950 </font></td>
- <td align=right width=80><font size=2 face="New Times Roman,Times,Serif"> 51.950 </font></td>
- """
-
-from pyparsing import *
-
-tdS,tdE = makeHTMLTags("TD")
-fontS,fontE = makeHTMLTags("FONT")
-realNum = Combine( Word(nums) + "." + Word(nums) ).setParseAction(lambda t:float(t[0]))
-NBSP = Literal(" ")
-patt = tdS + fontS + NBSP + realNum("value") + NBSP + fontE + tdE
-
-tdS.setParseAction( withAttribute(align="right",width="80") )
-for s in patt.searchString(data):
- print(s.value)
+# +# withAttribute.py +# Copyright, 2007 - Paul McGuire +# +# Simple example of using withAttribute parse action helper +# to define +# +import pyparsing as pp + +data = """\ + <td align=right width=80><font size=2 face="New Times Roman,Times,Serif"> 49.950 </font></td> + <td align=left width=80><font size=2 face="New Times Roman,Times,Serif"> 50.950 </font></td> + <td align=right width=80><font size=2 face="New Times Roman,Times,Serif"> 51.950 </font></td> + """ + +td, tdEnd = pp.makeHTMLTags("TD") +font, fontEnd = pp.makeHTMLTags("FONT") +realNum = pp.pyparsing_common.real +NBSP = pp.Literal(" ") +patt = td + font + NBSP + realNum("value") + NBSP + fontEnd + tdEnd + +# always use addParseAction when adding withAttribute as a parse action to a start tag +td.addParseAction(pp.withAttribute(align="right", width="80")) + +for s in patt.searchString(data): + print(s.value) |