我在使用 Python 从 XML 文件中以正确的顺序提取正确的数据时遇到问题

I'm having trouble extracting the right data in the right order from an XML file using Python

提问人:Elliobu 提问时间:2/15/2023 最后编辑:mzjnElliobu 更新时间:3/2/2023 访问量:29

问:

这是我写的代码片段:

from lxml import etree as lxmlET
lxmltree = lxmlET.parse('sample.xml')
for names in lxmltree.xpath("/data/country/name"):
    print(names.text)
    for element in lxmltree.iter('country'):
        for element2 in lxmltree.iter('rank'):
            print(element2.text)

我需要的输出:

名称wsf 2

3

4

5

NAMEWSF2

6

7

8

9 NAMEWSF3

10

我得到的输出: 名称wsf 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 NAMEWSF2 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 NAMEWSF3 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10

这是XML文件:sample.xml

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <name>NAMEWSF</name>
        <rank updated="yes">2</rank>
        <rank updated="yes">3</rank>
        <rank updated="yes">4</rank>
        <rank updated="yes">5</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <name>NAMEWSF2</name>
        <rank updated="yes">6</rank>
        <rank updated="yes">7</rank>
        <rank updated="yes">8</rank>
        <rank updated="yes">9</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <name>NAMEWSF3</name>
        <rank updated="yes">10</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

我尝试同时使用 xml 和 lxml,但我在文档中找不到可以帮助我的东西,我认为这只是代码结构的问题,我是编程新手,所以我仍在努力把它做好

python-3.x xml 解析 lxml

评论


答:

0赞 Jack Fleeting 2/15/2023 #1

如果我理解正确的话,您可能正在寻找这样的东西:

for country in lxmltree.xpath('//country'):
   print(country.xpath('.//name/text()') + country.xpath('.//rank/text()'))

输出:

['NAMEWSF', '2', '3', '4', '5']
['NAMEWSF2', '6', '7', '8', '9']
['NAMEWSF3', '10']