Python XML解析器迭代特定标记

n3schb8v  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(169)

下面是我的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标签?
非常感谢你的帮助。

hmae6n7t

hmae6n7t1#

编辑:为了复制,我修复了您的xml:
xml_data:

<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>
            <State name = "Texas">
                <Info> This is the info of Texas </Info>
                <population number = "60"  area = "2000" > </population>
            </State>
        </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>
            <State name = "TG">
                <Info> This is the info of TG </Info>
                <population number = "30"  area = "500" > </population>
            </State>
        </country>           
    </value>
</Attributes>

然后可以使用嵌套循环:

import xml.etree.ElementTree as ET
xml_value = ET.fromstring(xml_data)

for country in xml_value.findall('./value/country'):
    for state in country.findall('State'):
        pop = state.find('population')
        print(country.attrib['name'], state.attrib['name'], pop.attrib['number'], pop.attrib['area'])

相关问题