mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Issue #23921: Standardized documentation whitespace formatting.
Original patch by James Edwards.
This commit is contained in:
parent
387235085c
commit
dba903993a
63 changed files with 445 additions and 409 deletions
|
@ -94,7 +94,7 @@ As an :class:`Element`, ``root`` has a tag and a dictionary of attributes::
|
|||
It also has children nodes over which we can iterate::
|
||||
|
||||
>>> for child in root:
|
||||
... print(child.tag, child.attrib)
|
||||
... print(child.tag, child.attrib)
|
||||
...
|
||||
country {'name': 'Liechtenstein'}
|
||||
country {'name': 'Singapore'}
|
||||
|
@ -143,8 +143,8 @@ elements, call :meth:`XMLPullParser.read_events`. Here is an example::
|
|||
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
|
||||
>>> parser.feed(' more text</mytag>')
|
||||
>>> for event, elem in parser.read_events():
|
||||
... print(event)
|
||||
... print(elem.tag, 'text=', elem.text)
|
||||
... print(event)
|
||||
... print(elem.tag, 'text=', elem.text)
|
||||
...
|
||||
end
|
||||
|
||||
|
@ -166,7 +166,7 @@ the sub-tree below it (its children, their children, and so on). For example,
|
|||
:meth:`Element.iter`::
|
||||
|
||||
>>> for neighbor in root.iter('neighbor'):
|
||||
... print(neighbor.attrib)
|
||||
... print(neighbor.attrib)
|
||||
...
|
||||
{'name': 'Austria', 'direction': 'E'}
|
||||
{'name': 'Switzerland', 'direction': 'W'}
|
||||
|
@ -180,9 +180,9 @@ with a particular tag, and :attr:`Element.text` accesses the element's text
|
|||
content. :meth:`Element.get` accesses the element's attributes::
|
||||
|
||||
>>> for country in root.findall('country'):
|
||||
... rank = country.find('rank').text
|
||||
... name = country.get('name')
|
||||
... print(name, rank)
|
||||
... rank = country.find('rank').text
|
||||
... name = country.get('name')
|
||||
... print(name, rank)
|
||||
...
|
||||
Liechtenstein 1
|
||||
Singapore 4
|
||||
|
@ -206,9 +206,9 @@ Let's say we want to add one to each country's rank, and add an ``updated``
|
|||
attribute to the rank element::
|
||||
|
||||
>>> for rank in root.iter('rank'):
|
||||
... new_rank = int(rank.text) + 1
|
||||
... rank.text = str(new_rank)
|
||||
... rank.set('updated', 'yes')
|
||||
... new_rank = int(rank.text) + 1
|
||||
... rank.text = str(new_rank)
|
||||
... rank.set('updated', 'yes')
|
||||
...
|
||||
>>> tree.write('output.xml')
|
||||
|
||||
|
@ -244,9 +244,9 @@ We can remove elements using :meth:`Element.remove`. Let's say we want to
|
|||
remove all countries with a rank higher than 50::
|
||||
|
||||
>>> for country in root.findall('country'):
|
||||
... rank = int(country.find('rank').text)
|
||||
... if rank > 50:
|
||||
... root.remove(country)
|
||||
... rank = int(country.find('rank').text)
|
||||
... if rank > 50:
|
||||
... root.remove(country)
|
||||
...
|
||||
>>> tree.write('output.xml')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue