Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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
在XML python中获取子节点_Python_Xml - Fatal编程技术网

在XML python中获取子节点

在XML python中获取子节点,python,xml,Python,Xml,我是python和XML的新手,目前正在使用Python3.6,我在变量中有这些XML数据 <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc>

我是python和XML的新手,目前正在使用Python3.6,我在变量中有这些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>

1.
2008
141100
4.
2011
59900
68
2011
13600
我想做的是获取“子节点”?然后把它放在一个像这样的变量上

var1 = '<country name="Liechtenstein">
    <rank>1</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
</country>'

var2 = '<country name="Singapore">
    <rank>4</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
</country>'

...etc
var1='1'
1.
2008
141100
'
var2=
4.
2011
59900
'
等

有没有办法得到结果

您可能不想将每个国家的数据分配给它自己的变量,除非您绝对确定它们的数量很小并且没有变化。因此,使用循环处理每个国家最简单:

data = ...
from xml.etree import ElementTree
tree = ElementTree.fromstring(data)
countries = tree.findall("country")
您现在有了一个国家列表,您可以反复查看以进一步分析每个国家

for country in countries:
    print(country)

<Element 'country' at 0x10e51d818>
<Element 'country' at 0x10e535688>
<Element 'country' at 0x10e535cc8>
适用于国家/地区中的国家/地区:
印刷品(国家)

以下是我解决这个问题的方法:

您可以创建一个所有国家元素的列表,然后在该列表上执行一些操作,因为@holdenweb已经提到,在您拥有的每个xml中,国家节点可能是可变的。因此,我使用一个列表将节点保留在该列表中。您可以根据自己的需求迭代该列表

代码:

import xml.etree.ElementTree as ET

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>"""

nodeList = []
root = ET.fromstring(xml)
for nod in root.findall("country"):
    nodeList.append(ET.tostring(nod))
    print(str(ET.tostring(nod) + b"\n"))
将xml.etree.ElementTree作为ET导入
xml=”“”
1.
2008
141100
4.
2011
59900
68
2011
13600
"""
节点列表=[]
root=ET.fromstring(xml)
对于根中的nod.findall(“国家”):
nodeList.append(ET.tostring(nod))
打印(str(ET.tostring(nod)+b“\n”))
输出:

b'<country name="Liechtenstein">\n        <rank>1</rank>\n        <year>2008</year>\n        <gdppc>141100</gdppc>\n        <neighbor direction="E" name="Austria" />\n        <neighbor direction="W" name="Switzerland" />\n    </country>\n    \n'
b'<country name="Singapore">\n        <rank>4</rank>\n        <year>2011</year>\n        <gdppc>59900</gdppc>\n        <neighbor direction="N" name="Malaysia" />\n    </country>\n    \n'
b'<country name="Panama">\n        <rank>68</rank>\n        <year>2011</year>\n        <gdppc>13600</gdppc>\n        <neighbor direction="W" name="Costa Rica" />\n        <neighbor direction="E" name="Colombia" />\n    </country>\n\n'
b'\n 1\n 2008\n 141100\n\n\n\n\n'
b'\n 4\n 2011\n 59900\n\n\n\n'
b'\n 68\n 2011\n 13600\n\n\n\n\n'

感谢您的快速响应,可以作为字符串获取?@jaycelcunan
ET.tostring(country)
您可以尝试此项。您想要整个元素XML的字符串,还是提取单个标记/属性值?请尽量具体一点。