【问题标题】:How to get data of a specific root in XML(by python)如何在 XML 中获取特定根的数据(通过 python)
【发布时间】:2018-07-23 11:01:16
【问题描述】:

我想通过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'}

只有。

我应该使用哪个函数?

【问题讨论】:

标签: python xml excel elementtree


【解决方案1】:

你可以这样做:

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

【讨论】:

    【解决方案2】:

    安装

    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。玩得开心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 2014-01-28
      • 2011-08-22
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 2016-01-31
      相关资源
      最近更新 更多