Skip to content

Instantly share code, notes, and snippets.

@donlovett
Created September 22, 2016 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donlovett/c11b56794f58a444653983a38df03542 to your computer and use it in GitHub Desktop.
Save donlovett/c11b56794f58a444653983a38df03542 to your computer and use it in GitHub Desktop.
Read xml file and sum an attribute for Coursera Week 5
import xml.etree.ElementTree as ET
#fname = raw_input('Enter the file name: ')
#fname = '/home/vagrant/Coursera/Week5/comments_42.xml'
fname = '/home/vagrant/Coursera/Week5/comments_318170.xml'
try:
tree = ET.ElementTree(file=fname)
except:
print 'File cannot be opened:', fname
exit()
root = tree.getroot()
mycounter = 0
mysum = 0
#stuff = ET.fromstring(input)
lst = root.findall('comments/comment')
print 'Comment count:', len(lst)
for item in lst:
#print 'Name', item.find('name').text
#print 'Count', item.find('count').text
mycounter = mycounter + 1
mysum = mysum + int(item.find('count').text)
print mycounter
print mysum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment