Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python通过xml.tree获取元素?_Python_Python 3.x_Xml - Fatal编程技术网

如何使用python通过xml.tree获取元素?

如何使用python通过xml.tree获取元素?,python,python-3.x,xml,Python,Python 3.x,Xml,我正在学习如何使用xml.etree解析xml文件中的数据,但似乎缺少了重要信息 我用的是同一个例子: <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>14

我正在学习如何使用
xml.etree
解析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>
但这些似乎都不起作用。如何提取这三个国家的

预期产出:

country {'name': 'Liechtenstein'} 2008
country {'name': 'Singapore'} 2011
country {'name': 'Panama'} 2011
import sys
import xml.etree.ElementTree as ET

tree = ET.parse(sys.argv[1])
root = tree.getroot()

for child in root.iter('country'):
    for grandchild in child.iter('year'):
        print(child.attrib, grandchild.text)
附录

我找到了一个获得“年”的方法:

import sys
import xml.etree.ElementTree as ET

tree = ET.parse(sys.argv[1])
root = tree.getroot()
for child in root:
    for elem in list(child):
        if elem.tag == 'year':
            print(child.tag, child.attrib, elem.text)

没有更简单的方法吗?

你的方向是对的:) 试一试

关于您的尝试的一些注意事项:

  • child.get(attribute\u name)
    返回元素
    child
    的名为
    attribute\u name
    的属性
  • child[]
    需要一个索引(即整数)

使用哪种python版本?对于python 3.8,它将是:

def get_value(el):
    return el.text if el is not None else None

root = ET.fromstring(xml)

for country in root.findall('country'):
    year = get_value(country.find('year'))
    rank = get_value(country.find('rank'))
    neighbors = country.findall('neighbor')
    neighbor_names = [neighbor.get('name') for neighbor in neighbors]
    print(year, rank, neighbor_names)

看看Element.iter()方法

以下代码段将为您提供所需的输出:

country {'name': 'Liechtenstein'} 2008
country {'name': 'Singapore'} 2011
country {'name': 'Panama'} 2011
import sys
import xml.etree.ElementTree as ET

tree = ET.parse(sys.argv[1])
root = tree.getroot()

for child in root.iter('country'):
    for grandchild in child.iter('year'):
        print(child.attrib, grandchild.text)

使用
child.find()
时得到了什么输出?如果只有第一个元素,则该方法按照文档中的预期工作:find(match,namespaces=None)查找第一个匹配的子元素。匹配可能是标记名或路径。抱歉,似乎有效。我很确定之前发出了错误信息。。。
{'Liechtenstein': '2008', 'Singapore': '2011', 'Panama': '2011'}