Reputation: 1016
Trying to extend a list with python and really in struggle town. Basically I'm pulling XML data and then taking what I need into a list and then trying to save it in a file. I plan on taking the data every 30 minutes (creating road congestion data ! :D).
Really appreciate any help - this is all a big learning curve for me.
Cheers - Here is the data below. I've given a similar XML data as the data I have has a private key.
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>
One of the examples given is great but is giving me this output ( from 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']
>>>
Upvotes: 0
Views: 160
Reputation: 44465
Depending on what you want, try:
data.extend(item + " : " + elem.text)
or
data.append(item + " : " + elem.text)
Upvotes: 1