解析xml的问题

时间:2015-01-04 03:17:32

标签: python xml csv lxml

我有一些代码正在解析xml文件并将其保存为csv。我可以这两种方式,一种是通过手动下载xml文件然后解析它,另一种是通过使用ET.fromstring直接获取xml feed然后解析。当我直接去我得到数据错误时,它似乎是一个完整性问题。我试图将xml下载包含在代码中,但我不太确定解决此问题的最佳方法。

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

url = 'http://www.capitalbikeshare.com/data/stations/bikeStations.xml'
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()

1 个答案:

答案 0 :(得分:1)

您需要的是:

root = ET.fromstring(data)

并省略以下行:tree = ET.parse('bikeStations.xml')

connection.read()的响应返回字符串时,您可以使用 fromstring 方法直接读取XML字符串,您可以从{{3}中阅读更多内容}。