Using ElementTree to find ‘count’ tag and sum them

Some small piece of code:

import urllib
import xml.etree.ElementTree as ET

sum = 0

url = raw_input('Enter location: ')
if len(url) < 1 : exit()

print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
#print data

tree = ET.fromstring(data)
results = tree.findall('.//count')

for i in range(0, len(results)):
 sum = sum + int(results[i].text)
 
print "Count: %i" % len(results)
print "Sum: %i" % sum
Advertisements

PrettyPrint

Prettyprint (or pretty-print) is the application of any of various stylistic formatting conventions to text files, such as source code, markup, and similar kinds of content. These formatting conventions can adjust positioning and spacing (indent style), add color and contrast (syntax highlighting), adjust size, and make similar modifications intended to make the content easier for people to view, read, and understand. Prettyprinters for programming language source code are sometimes called code beautifiers or syntax highlighters (Wikipedia).

JSON Pretty Print: http://jsonprettyprint.com/

Another JSON Pretty Print: http://www.prettyprint.net/

XML Pretty Print: http://xmlprettyprint.com/