如何在XML(by python)中获取特定根的数据
How to get data of a specific root in XML(by python)
我想通过 xml.etree.ElementTree
将 xml 文件转换为 excel。
我想从特定的根读取数据。
假设我的 xml 看起来像
<?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>
如果我直接使用'iter',我会得到:
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'}
但我只想获取“列支敦士登”的所有邻居
这意味着我希望我的脚本给我
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
仅。
我应该使用哪个函数?
你可以做到:
x = """<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>
"""
import xml.etree.ElementTree as ET
data = ET.fromstring(x) //here x is xml string
for child in data:
if child.attrib['name'] == 'Liechtenstein':
for grandchild in child:
if grandchild.tag == 'neighbor':
print grandchild.attrib
安装
pip3 install lxml
代码
from lxml import etree
with open('countries.xml', 'r') as f:
root = etree.fromstring(f.read())
neighbors = root.xpath('/data/country[@name="Liechtenstein"]/neighbor')
for n in neighbors:
n_names = n.xpath('@name')
n_name = n_names[0]
n_directions = n.xpath('@direction')
n_direction = n_directions[0]
print(n_name, n_direction)
输出
Austria E
Switzerland W
在 Python 3.6.0.
上测试
如果您使用 Python 2.7:将 pip3
替换为 pip
,将 print(n_name, n_direction)
替换为 print n_name, n_direction
。玩得开心。
我想通过 xml.etree.ElementTree
将 xml 文件转换为 excel。
我想从特定的根读取数据。
假设我的 xml 看起来像
<?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>
如果我直接使用'iter',我会得到:
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'}
但我只想获取“列支敦士登”的所有邻居 这意味着我希望我的脚本给我
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
仅。
我应该使用哪个函数?
你可以做到:
x = """<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>
"""
import xml.etree.ElementTree as ET
data = ET.fromstring(x) //here x is xml string
for child in data:
if child.attrib['name'] == 'Liechtenstein':
for grandchild in child:
if grandchild.tag == 'neighbor':
print grandchild.attrib
安装
pip3 install lxml
代码
from lxml import etree
with open('countries.xml', 'r') as f:
root = etree.fromstring(f.read())
neighbors = root.xpath('/data/country[@name="Liechtenstein"]/neighbor')
for n in neighbors:
n_names = n.xpath('@name')
n_name = n_names[0]
n_directions = n.xpath('@direction')
n_direction = n_directions[0]
print(n_name, n_direction)
输出
Austria E
Switzerland W
在 Python 3.6.0.
上测试如果您使用 Python 2.7:将 pip3
替换为 pip
,将 print(n_name, n_direction)
替换为 print n_name, n_direction
。玩得开心。