@@ -5,8 +5,8 @@ Using the pyparsing module
5
5
:author: Paul McGuire
6
6
:address: ptmcg@users.sourceforge.net
7
7
8
- :revision: 3.0.0
9
- :date: August, 2020
8
+ :revision: 3.0.1
9
+ :date: May, 2021
10
10
11
11
:copyright: Copyright |copy | 2003-2020 Paul McGuire.
12
12
@@ -765,16 +765,46 @@ Other classes
765
765
own list structure, so that the tokens can be handled as a hierarchical
766
766
tree
767
767
768
+ - as an object
769
+
770
+ - named elements can be accessed as if they were attributes of an object:
771
+ if an element is referenced that does not exist, it will return ``"" ``.
772
+
768
773
ParseResults can also be converted to an ordinary list of strings
769
774
by calling ``asList() ``. Note that this will strip the results of any
770
775
field names that have been defined for any embedded parse elements.
771
776
(The ``pprint `` module is especially good at printing out the nested contents
772
777
given by ``asList() ``.)
773
778
774
- Finally, ParseResults can be viewed by calling ``dump() ``. ``dump()` will first show
779
+ Finally, ParseResults can be viewed by calling ``dump() ``. ``dump() `` will first show
775
780
the ``asList() `` output, followed by an indented structure listing parsed tokens that
776
781
have been assigned results names.
777
782
783
+ Here is sample code illustrating some of these methods::
784
+
785
+ >>> number = Word(nums)
786
+ >>> name = Combine(Word(alphas)[...], adjacent=False, joinString=" ")
787
+ >>> parser = number("house_number") + name("street_name")
788
+ >>> result = parser.parseString("123 Main St")
789
+ >>> print(result)
790
+ ['123', 'Main St']
791
+ >>> print(type(result))
792
+ <class 'pyparsing.ParseResults'>
793
+ >>> print(repr(result))
794
+ (['123', 'Main St'], {'house_number': ['123'], 'street_name': ['Main St']})
795
+ >>> result.house_number
796
+ '123'
797
+ >>> result["street_name"]
798
+ 'Main St'
799
+ >>> result.asList()
800
+ ['123', 'Main St']
801
+ >>> result.asDict()
802
+ {'house_number': '123', 'street_name': 'Main St'}
803
+ >>> print(result.dump())
804
+ ['123', 'Main St']
805
+ - house_number: '123'
806
+ - street_name: 'Main St'
807
+
778
808
779
809
Exception classes and Troubleshooting
780
810
-------------------------------------
0 commit comments