下面是我的XML数据的样子
XML数据
<Attributes>
<value>
<country name = "America" >
<Info> This is the info of America </Info>
<State name = Missouri>
<Info> This is the info of Missouri </Info>
<population number = "50" area = "1000 > </population>
<State name = Texas>
<Info> This is the info of Texas </Info>
<population number = "60" area = "2000 > </population>
</country>
<country name = "India" >
<Info> This is the info of India </Info>
<State name = UP>
<Info> This is the info of UP </Info>
<population number = "150" area = "3000 > </population>
<State name = TG>
<Info> This is the info of TG </Info>
<population number = "30" area = "500 > </population>
</country>
</value>
</Attributes>
我想从我的Python程序输出如下
America Missouri 50 1000
America Texas 60 2000
India UP 150 3000
India UP 30 500
我已经写了下面的代码,但不满意,因为我循环通过每个标签和存储的值。
import xml.etree.ElementTree as ET
xml_value = ET.fromstring(xml_data)
for line in xml_value.iter():
if line.tag == 'country':
country_name = line.attrib['name']
elif line.tag == 'State':
st_name = line.attrib['name']
elif line.tag == 'population':
print(country_name, st_name, line.attrib['number'], line.attrib['area']))
是否有一种方法可以直接迭代到Country标签,然后迭代到State和population,而不是转到Info标签?
非常感谢你的帮助。
1条答案
按热度按时间hmae6n7t1#
编辑:为了复制,我修复了您的xml:
xml_data:
然后可以使用嵌套循环: