Problems with parsing xml



I have some code that is parsing an xml file and saving it as a csv. I can do this two ways, one by manually downloading the xml file and then parsing it, the other by taking the xml feed directly using ET.fromstring and then parsing. When I go directly I get data errors it appears to be an integrity issue. I am trying to include the xml download in to the code, but I am not quite sure the best way to approach this.



import xml.etree.ElementTree as ET
import csv
import urllib

url = 'http://ift.tt/13zT9Qg'
connection = urllib.urlopen(url)
data = connection.read()

#I need code here!!!

tree = ET.parse('bikeStations.xml')
root = tree.getroot()

#for child in root:
#print child.tag, child.attrib

locations = []

for station in root.findall('station'):
name = station.find('name').text
bikes = station.find('nbBikes').text
docks = station.find('nbEmptyDocks').text
time = station.find('latestUpdateTime').text
sublist = [name, bikes, docks, time]
locations.append(sublist)
#print 'Station:', name, 'has', bikes, 'bikes and' ,docks, 'docks'

#print locations

s = open('statuslog.csv', 'wb')
w = csv.writer(s)
w.writerows(locations)
s.close()

f = open('filelog.csv', 'ab')
w = csv.writer(f)
w.writerows(locations)
f.close()

No comments:

Post a Comment