diff options
author | Paul McGuire <ptmcg@users.noreply.github.com> | 2018-01-06 23:38:53 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-06 23:38:53 -0600 |
commit | 430c5ad767cc946e9da7cd5f4673a4e3bd135a3c (patch) | |
tree | 5a7df11e0fd52ab320b0ef3e670e260f315ca9ae /examples/parseTabularData.py | |
parent | f1d12567a8da4d254e6d62bb0d650c87c7d0bb89 (diff) | |
parent | d953150a6db3ac247a64b047edc2df7156f3e56b (diff) | |
download | pyparsing-git-430c5ad767cc946e9da7cd5f4673a4e3bd135a3c.tar.gz |
Merge pull request #1 from cngkaygusuz/master
Add Scrutinizer-CI configuration and other niceties
Diffstat (limited to 'examples/parseTabularData.py')
-rw-r--r-- | examples/parseTabularData.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/parseTabularData.py b/examples/parseTabularData.py new file mode 100644 index 0000000..3846310 --- /dev/null +++ b/examples/parseTabularData.py @@ -0,0 +1,50 @@ +#
+# parseTabularData.py
+#
+# Example of parsing data that is formatted in a tabular listing, with
+# potential for missing values. Uses new addCondition method on
+# ParserElements.
+#
+# Copyright 2015, Paul McGuire
+#
+from pyparsing import col,Word,Optional,alphas,nums,ParseException
+
+table = """\
+ 1 2
+12345678901234567890
+COLOR S M L
+RED 10 2 2
+BLUE 5 10
+GREEN 3 5
+PURPLE 8"""
+
+# function to create column-specific parse conditions
+def mustMatchCols(startloc,endloc):
+ return lambda s,l,t: startloc <= col(l,s) <= endloc
+
+# helper to define values in a space-delimited table
+# (change empty_cell_is_zero to True if a value of 0 is desired for empty cells)
+def tableValue(expr, colstart, colend):
+ empty_cell_is_zero = False
+ if empty_cell_is_zero:
+ return Optional(expr.copy().addCondition(mustMatchCols(colstart,colend),
+ message="text not in expected columns"),
+ default=0)
+ else:
+ return Optional(expr.copy().addCondition(mustMatchCols(colstart,colend),
+ message="text not in expected columns"))
+
+
+# define the grammar for this simple table
+colorname = Word(alphas)
+integer = Word(nums).setParseAction(lambda t: int(t[0])).setName("integer")
+row = (colorname("name") +
+ tableValue(integer, 11, 12)("S") +
+ tableValue(integer, 15, 16)("M") +
+ tableValue(integer, 19, 20)("L"))
+
+# parse the sample text - skip over the header and counter lines
+for line in table.splitlines()[3:]:
+ print(line)
+ print(row.parseString(line).dump())
+ print('')
|