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/dictExample.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/dictExample.py')
-rw-r--r-- | examples/dictExample.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/dictExample.py b/examples/dictExample.py new file mode 100644 index 0000000..5085aed --- /dev/null +++ b/examples/dictExample.py @@ -0,0 +1,41 @@ +#
+# dictExample.py
+#
+# Illustration of using pyparsing's Dict class to process tabular data
+#
+# Copyright (c) 2003, Paul McGuire
+#
+from pyparsing import Literal, Word, Group, Dict, ZeroOrMore, alphas, nums, delimitedList
+import pprint
+
+testData = """
++-------+------+------+------+------+------+------+------+------+
+| | A1 | B1 | C1 | D1 | A2 | B2 | C2 | D2 |
++=======+======+======+======+======+======+======+======+======+
+| min | 7 | 43 | 7 | 15 | 82 | 98 | 1 | 37 |
+| max | 11 | 52 | 10 | 17 | 85 | 112 | 4 | 39 |
+| ave | 9 | 47 | 8 | 16 | 84 | 106 | 3 | 38 |
+| sdev | 1 | 3 | 1 | 1 | 1 | 3 | 1 | 1 |
++-------+------+------+------+------+------+------+------+------+
+"""
+
+# define grammar for datatable
+heading = (Literal(
+"+-------+------+------+------+------+------+------+------+------+") +
+"| | A1 | B1 | C1 | D1 | A2 | B2 | C2 | D2 |" +
+"+=======+======+======+======+======+======+======+======+======+").suppress()
+vert = Literal("|").suppress()
+number = Word(nums)
+rowData = Group( vert + Word(alphas) + vert + delimitedList(number,"|") + vert )
+trailing = Literal(
+"+-------+------+------+------+------+------+------+------+------+").suppress()
+
+datatable = heading + Dict( ZeroOrMore(rowData) ) + trailing
+
+# now parse data and print results
+data = datatable.parseString(testData)
+print(data)
+pprint.pprint(data.asList())
+print("data keys=", list(data.keys()))
+print("data['min']=", data['min'])
+print("data.max", data.max)
|