I am new to python and learning to read in xml files. Running my code to append the items gives me repeated arrays. I guess it is looping through all the items appending and returning an array each time. I just want one array the array of all the items. What am I doing wrong?
The code is as follows:
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)
The result is as follows:
['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']
I just want the last array. Thanks for the help