我是python和XML的新手,目前我正在使用python 3.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>
我想要做的是获得&#34;子节点&#34;?并将它放在像这样的变量
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
有没有办法得到结果
答案 0 :(得分:1)
以下是解决此问题的方法:
您可以列出所有国家/地区元素,然后在该列表上执行一些操作,因为@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"))
输出:
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'
答案 1 :(得分:0)
您可能不希望将每个国家/地区的数据分配给自己的变量,除非您完全确定它们的数量很小并且不会发生变化。因此,最简单的方法是使用循环来处理每个国家:
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>