尝试使用python扩展列表,并且真的在奋斗的城镇。 基本上我正在提取XML数据,然后将我需要的内容放入列表中,然后尝试将其保存在文件中。我计划每30分钟获取一次数据(创建道路拥堵数据!:D)。
非常感谢任何帮助 - 这对我来说是一个很大的学习曲线。
干杯 - 以下是数据。我给出了类似的XML数据,因为我拥有私钥的数据。
from lxml import etree
import urllib.request
import pickle
#append to list next
def handleLeg(leg):
# print this leg as text, or save it to file maybe...
text = etree.tostring(leg, pretty_print=True)
# also process individual elements of interest here if we want
tagsOfInterest=["noTrafficTravelTimeInSeconds", "lengthInMeters", "departureTime", "trafficDelayInSeconds"] # whatever
#list to use for data analysis
global data
data = []
for child in leg:
if 'summary' in child.tag:
for elem in child:
for item in tagsOfInterest:
if item in elem.tag:
print (item + " : " + elem.text)
"""
Traceback (most recent call last):
File "/home/Hewlbern/noobie/newfile.py", line 40, in <module>
parseXML("xmlFile")
File "/home/Hewlbern/noobie/newfile.py", line 32, in parseXML
handleLeg(child)
File "/home/Hewlbern/noobie/newfile.py", line 20, in handleLeg
data.extend = (item + " : " + elem.text)
AttributeError: 'list' object attribute 'extend' is read-only
"""
data.extend = (item + " : " + elem.text)
def parseXML(xmlFile):
"""
Parse the xml
"""
with urllib.request.urlopen("https://api.tomtom.com/routing/1/calculateRoute/-37.79205923474775,145.03010268799338:-37.798883995180496,145.03040309540322:-37.807106781970354,145.02895470253526:-37.80320743019992,145.01021142594075:-37.7999012967757,144.99318476311566:?routeType=shortest&key=NOKEYBISHcomputeTravelTimeFor=all") as fobj:
xml = fobj.read()
#Look at Parent and Child XML organisation as this is where the data is going wrong at the moment
root = etree.fromstring(xml)
for child in root:
if 'route' in child.tag:
handleLeg(child)
"""for elem in child:
if 'leg' in elem.tag:
handleLeg(elem)
"""
if __name__ == "__main__":
parseXML("xmlFile")
pickling_on = open("data.pickle","wb")
pickle.dump(data, pickling_on)
pickling_on.close()
pickle_off = open("data.pickle","rb")
data = pickle.load(pickle_off)
print(data)
<summary>
<lengthInMeters>5144</lengthInMeters>
<travelTimeInSeconds>764</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2017-12-28T14:42:14+11:00</departureTime>
<arrivalTime>2017-12-28T14:54:58+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>478</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>764</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>764</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
<leg>
<summary>
<lengthInMeters>806</lengthInMeters>
<travelTimeInSeconds>67</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2017-12-28T14:42:14+11:00</departureTime>
<arrivalTime>2017-12-28T14:43:21+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>59</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>67</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>67</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
给出的一个例子很棒但是给了我这个输出(来自data.extend(item +“:”+ elem.text))
['l', 'e', 'n', 'g', 't', 'h', 'I', 'n', 'M', 'e', 't', 'e', 'r', 's', ' ', ':', ' ', '5', '1', '4', '5', 't', 'r', 'a', 'f', 'f', 'i', 'c', 'D', 'e', 'l', 'a', 'y', 'I', 'n', 'S', 'e', 'c', 'o', 'n', 'd', 's
', ' ', ':', ' ', '2', '8', '2', 'd', 'e', 'p', 'a', 'r', 't', 'u', 'r', 'e', 'T', 'i', 'm', 'e', ' ', ':', ' ', '2', '0', '1', '8', '-', '0', '1', '-', '2', '4', 'T', '1', '7', ':', '1', '2', ':', '5', '2',
'+', '1', '1', ':', '0', '0', 'n', 'o', 'T', 'r', 'a', 'f', 'f', 'i', 'c', 'T', 'r', 'a', 'v', 'e', 'l', 'T', 'i', 'm', 'e', 'I', 'n', 'S', 'e', 'c', 'o', 'n', 'd', 's', ' ', ':', ' ', '4', '8', '9']
>>>
答案 0 :(得分:1)
根据您的需要,尝试:
data.extend(item + " : " + elem.text)
或
data.append(item + " : " + elem.text)