I've been given an assignment where I have to extract the numbers out of this xml file (https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/comments_42.xml) and then sum them up. The problem is I'm getting an attribute error when I try to do a for loop to get at the data.
import urllib import xml.etree.ElementTree as ET url = raw_input('Enter location: ') print 'Retrieving', url uh = urllib.urlopen(url) data = uh.read() print 'Retrieved',len(data),'characters' tree = ET.fromstring(data) lst = tree.findall('.//count') print 'Count:', len(lst) for item in lst: print 'name', item.find('count').text
I should be extracting text from inside the count tag:
<comment> <name>Matthias</name> <count>97</count> </comment>
Can anyone show me how it's done? Thanks in advance.
No comments:
Post a Comment