Created
September 22, 2016 17:01
-
-
Save donlovett/c11b56794f58a444653983a38df03542 to your computer and use it in GitHub Desktop.
Read xml file and sum an attribute for Coursera Week 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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