解析问题 xml

Problems with parsing xml

我有一些代码正在解析 xml 文件并将其保存为 csv。我可以通过两种方式做到这一点,一种是手动下载 xml 文件然后解析它,另一种是直接使用 ET.fromstring 获取 xml 提要然后解析。当我直接去时,我收到数据错误,这似乎是一个完整性问题。我正在尝试将 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()

您需要的是:

root = ET.fromstring(data)

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

作为connection.read() returns String的响应,可以直接使用fromstring读取XML字符串 方法,您可以从 HERE.

阅读更多内容