Skip to content

Commit d108a29

Browse files
committed
Remove old language stating that parseString returns "a list of matched strings", and clarify use of the returned ParseResults
1 parent 5353ccd commit d108a29

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ The Python representation of the grammar is quite readable, owing to the
3636
self-explanatory class names, and the use of '+', '|' and '^' operator
3737
definitions.
3838
39-
The parsed results returned from ``parseString()`` can be accessed as a
39+
The parsed results returned from ``parseString()`` is a collection of type
40+
``ParseResults``, which can be accessed as a
4041
nested list, a dictionary, or an object with named attributes.
4142

4243
The pyparsing module handles some of the problems that are typically

docs/HowToUsePyparsing.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Using the pyparsing module
55
:author: Paul McGuire
66
:address: ptmcg@users.sourceforge.net
77

8-
:revision: 3.0.0
9-
:date: August, 2020
8+
:revision: 3.0.1
9+
:date: May, 2021
1010

1111
:copyright: Copyright |copy| 2003-2020 Paul McGuire.
1212

@@ -765,16 +765,46 @@ Other classes
765765
own list structure, so that the tokens can be handled as a hierarchical
766766
tree
767767

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+
768773
ParseResults can also be converted to an ordinary list of strings
769774
by calling ``asList()``. Note that this will strip the results of any
770775
field names that have been defined for any embedded parse elements.
771776
(The ``pprint`` module is especially good at printing out the nested contents
772777
given by ``asList()``.)
773778

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
775780
the ``asList()`` output, followed by an indented structure listing parsed tokens that
776781
have been assigned results names.
777782

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+
778808

779809
Exception classes and Troubleshooting
780810
-------------------------------------

0 commit comments

Comments
 (0)