python xml.etree.El

2020-01-14 16:56:19 浏览数 (1)

  1. Parsing XML
代码语言:javascript复制
<?xml version="1.0"?><data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country></data>

We have a number of ways to import the data. Reading the file from disk:

代码语言:javascript复制
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

Reading the data from a string:

代码语言:javascript复制
root = ET.fromstring(country_data_as_string)
代码语言:javascript复制
>>> root.tag
'data'
>>> root.attrib
{}
代码语言:javascript复制
>>> for child in root:
...   print child.tag, child.attrib
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
代码语言:javascript复制
>>> root[0][1].text
'2008'

2.Finding interesting elements

代码语言:javascript复制
>>> for neighbor in root.iter('neighbor'):
...   print neighbor.attrib
...
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}
代码语言:javascript复制
>>> for country in root.findall('country'):
...   rank = country.find('rank').text
...   name = country.get('name')
...   print name, rank
...
Liechtenstein 1
Singapore 4
Panama 68

0 人点赞