Ekaterina1234 Ekaterina1234 - 4 months ago 19
Python Question

Sum all xml tag values python

This code is supposed to sum the count of comments in this xml document.

import urllib
import xml.etree.ElementTree as ET



url = 'http://python-data.dr-chuck.net/comments_42.xml'


print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
print data
for line in data:
tree = ET.fromstring(data)
comments = tree.findall('comments')


name = comments[0].find('comment').find('name').text
count = comments[0].find('comment').find('count').text
count = int(count)
count = count + count
print count


but it only displays the number of comments on the first
comment
tag and then adds it to itself and then stops.

Here's the output. The top is the
xml
document and at the bottom there is the count(194, which is 97+97, only the comments by Romina), which is incorrect. It should be the sum of all comments in the file, not just Romina's

How to get the sum of all the comments in the file?

Retrieving http://python-data.dr-chuck.net/comments_42.xml
Retrieved 4189 characters
<?xml version="1.0" encoding="UTF-8"?>
<commentinfo>
<note>This file contains the sample data for testing</note>

<comments>
<comment>
<name>Romina</name>
<count>97</count>
</comment>
<comment>
<name>Laurie</name>
<count>97</count>
</comment>
<comment>
<name>Bayli</name>
<count>90</count>
</comment>
<comment>
<name>Siyona</name>
<count>90</count>
</comment>
<comment>
<name>Taisha</name>
<count>88</count>
</comment>
<comment>
<name>Ameelia</name>
<count>87</count>
</comment>
<comment>
<name>Alanda</name>
<count>87</count>
</comment>
<comment>
<name>Prasheeta</name>
<count>80</count>
</comment>
</commentinfo>

194

Answer

Note the subtle changes. Because <comments> contains several <comment> tags, we first need to find all of them ,only then we can iterate over them and find the <count> tag.

import urllib
import xml.etree.ElementTree as ET

url = 'http://python-data.dr-chuck.net/comments_42.xml'

uh = urllib.urlopen(url)
data = uh.read()

tree = ET.fromstring(data)
comments = tree.find('comments').findall('comment')
total_count = 0
for comment in comments:
    count = comment.find('count').text
    count = int(count)
    total_count += count
print total_count
>> 2553
Comments