Python将项附加到xml文件中的数组读取

Python将项附加到xml文件中的数组读取,python,xml,Python,Xml,我是python新手,正在学习阅读xml文件。 运行我的代码来附加这些项会给我重复的数组。我猜它是在所有项目中循环,每次都追加并返回一个数组。我只需要一个数组,所有项目的数组。我做错了什么 代码如下: countries_im = [] for country in root.findall('./country'): data = { 'name': None, 'infant_mortality': None }

我是python新手,正在学习阅读xml文件。 运行我的代码来附加这些项会给我重复的数组。我猜它是在所有项目中循环,每次都追加并返回一个数组。我只需要一个数组,所有项目的数组。我做错了什么

代码如下:

countries_im = []
for country in root.findall('./country'):
        data = {
            'name': None,
            'infant_mortality': None
        }
        data['name'] = country.find('./name').text
        data['infant_mortality'] = country.findtext('./infant_mortality')
        if data['infant_mortality'] is not None:
            countries_im.append(data['infant_mortality'])
            print(countries_im)
['13.19']
['13.19', '4.78']
['13.19', '4.78', '7.9']
['13.19', '4.78', '7.9', '6.16']
['13.19', '4.78', '7.9', '6.16', '3.69']
['13.19', '4.78', '7.9', '6.16', '3.69', '3.31']
结果如下:

countries_im = []
for country in root.findall('./country'):
        data = {
            'name': None,
            'infant_mortality': None
        }
        data['name'] = country.find('./name').text
        data['infant_mortality'] = country.findtext('./infant_mortality')
        if data['infant_mortality'] is not None:
            countries_im.append(data['infant_mortality'])
            print(countries_im)
['13.19']
['13.19', '4.78']
['13.19', '4.78', '7.9']
['13.19', '4.78', '7.9', '6.16']
['13.19', '4.78', '7.9', '6.16', '3.69']
['13.19', '4.78', '7.9', '6.16', '3.69', '3.31']

我只想要最后一个数组。感谢您的帮助

您的代码不会返回多个列表。事实上,它没有归还任何东西。您只有一个现有列表正在通过追加更改其内容

显示的结果是每次在列表中添加项目时调用
print(countries\u im)
的输出

如果将代码更改为:

countries_im = []
for country in root.findall('./country'):
        data = {
            'name': None,
            'infant_mortality': None
        }
        data['name'] = country.find('./name').text
        data['infant_mortality'] = country.findtext('./infant_mortality')
        if data['infant_mortality'] is not None:
            countries_im.append(data['infant_mortality'])
print(countries_im)

您将确认内存中的列表中附加了所有项目。

您的代码不会返回多个列表。事实上,它没有归还任何东西。您只有一个现有列表正在通过追加更改其内容

显示的结果是每次在列表中添加项目时调用
print(countries\u im)
的输出

如果将代码更改为:

countries_im = []
for country in root.findall('./country'):
        data = {
            'name': None,
            'infant_mortality': None
        }
        data['name'] = country.find('./name').text
        data['infant_mortality'] = country.findtext('./infant_mortality')
        if data['infant_mortality'] is not None:
            countries_im.append(data['infant_mortality'])
print(countries_im)

您将确认内存中的列表中附加了所有项。

如果我理解正确,您的代码是正确的,您只需将
print
语句移到
for
循环之外即可。换句话说,每次添加内容时都打印相同的数组。因此,在
for
循环(完成的数组)的末尾,您将得到您想要的结果

如果我理解正确,您的代码是正确的,您只需要将
print
语句移到
for
循环之外。换句话说,每次添加内容时都打印相同的数组。因此,在
for
循环(完成的数组)的末尾,您将得到您想要的结果

在for循环的每一步都打印阵列。将print语句移到for循环之外。在for循环的每个步骤都打印数组。将打印语句移到for-loop之外。您是正确的。如何将print语句移出循环,以便只打印最后一个数组?很抱歉延迟,请查看@david de la iglesia的答案,它是正确的。您是正确的。如何将print语句移出循环,以便只打印最后一个数组?很抱歉延迟,请查看@david de la iglesia的答案,这是正确的。