0

I am trying to use a while loop to make a script run every 10 or so minutes (in this example shorter for the sake of understanding if it's working).

It won't work and I'm not sure why - difficult to understand exactly what it is that isn't working. Basically it runs without an issue but nothing is going into the CSV file I am trying to save. Ideally I want it too save across a row, then go down another row with every loop. Thanks in advance!

from lxml import etree
import urllib.request
import csv
#import threading un-need at the moment.
import time

#Pickle is not needed
#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 = []
   #create header dictionary that includes the data to be appended within it. IE, Header = {TrafficDelay[data(0)]...etc
   for child in leg:
       if 'summary' in child.tag:
          for elem in child:
              for item in tagsOfInterest:
                  if item in elem.tag:
                      data.append(elem.text)



 #Parse the xml
#Threading way to run every couple of seconds
#threading.Timer(5.0, parseXML, ["xmlFile"]).start()
def parseXML(xmlFile):
    lastTime = time.time() - 10
    while time.time() >= lastTime + 10:
        lastTime += 10
        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=HerpinaLongbottom&computeTravelTimeFor=all") as fobj:
            xml = fobj.read()
        root = etree.fromstring(xml)
        for child in root:
            if 'route' in child.tag:
                handleLeg(child)

           # Write CSV file
                with open('datafile.csv', 'a') as fp:
                    writer = csv.writer(fp, delimiter=' ')
            # writer.writerow(["your", "header", "foo"])  # write header
                    writer.writerows(data)
                with open('datafile.csv', 'r') as fp:
                    reader = csv.reader(fp, quotechar='"')
        # next(reader, None)  # skip the headers
                    data_read = [row for row in reader]
                print(data_read)


if __name__ == "__main__":
   parseXML("xmlFile")

"""with open('datafile.csv', 'r') as fp:
    reader = csv.reader(fp, quotechar='"')
    # next(reader, None)  # skip the headers
    data_read = [row for row in reader]

print(data_read)"""

This is an example of the XML it's pulling as I obviously can't provide the code to the API i'm using.

<calculateRouteResponse xmlns="http://api.tomtom.com/routing" formatVersion="0.0.12">
<copyright>...</copyright>
<privacy>...</privacy>
<route>
<summary>
<lengthInMeters>5144</lengthInMeters>
<travelTimeInSeconds>687</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2018-01-16T11:16:06+11:00</departureTime>
<arrivalTime>2018-01-16T11:27:33+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>478</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>687</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>687</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
<leg>
<summary>
<lengthInMeters>806</lengthInMeters>
<travelTimeInSeconds>68</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2018-01-16T11:16:06+11:00</departureTime>
<arrivalTime>2018-01-16T11:17:14+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>59</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>68</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>68</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
<points>...</points>
</leg>
<leg>
<summary>
<lengthInMeters>958</lengthInMeters>
<travelTimeInSeconds>114</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2018-01-16T11:17:14+11:00</departureTime>
<arrivalTime>2018-01-16T11:19:08+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>77</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>114</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>114</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
<points>...</points>
</leg>
<leg>
<summary>
<lengthInMeters>1798</lengthInMeters>
<travelTimeInSeconds>224</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2018-01-16T11:19:08+11:00</departureTime>
<arrivalTime>2018-01-16T11:22:53+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>181</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>224</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>224</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
<points>...</points>
</leg>
<leg>
<summary>
<lengthInMeters>1582</lengthInMeters>
<travelTimeInSeconds>280</travelTimeInSeconds>
<trafficDelayInSeconds>0</trafficDelayInSeconds>
<departureTime>2018-01-16T11:22:53+11:00</departureTime>
<arrivalTime>2018-01-16T11:27:33+11:00</arrivalTime>
<noTrafficTravelTimeInSeconds>160</noTrafficTravelTimeInSeconds>
<historicTrafficTravelTimeInSeconds>280</historicTrafficTravelTimeInSeconds>
<liveTrafficIncidentsTravelTimeInSeconds>280</liveTrafficIncidentsTravelTimeInSeconds>
</summary>
<points>...</points>
</leg>
<sections>
<section>
<startPointIndex>0</startPointIndex>
<endPointIndex>139</endPointIndex>
<sectionType>TRAVEL_MODE</sectionType>
<travelMode>car</travelMode>
</section>
</sections>
</route>
</calculateRouteResponse>

Yet again Stack, appreciate any help. This task has upped my learning of python significantly - excited to get this little script working!

1 Answer 1

1

open's 'w' argument truncates the file each time it's invoked. You need an 'a'.

From https://docs.python.org/3/tutorial/inputoutput.html:

mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing.

Also, it seems that the script subsequent to lastTime += 10 should be indented to be in the timing loop, otherwise you never get there.

UPDATE:

Looking more closely, I think you mean something like:

lastTime = time.time() - 10
while True:       
    if time.time() >= lastTime + 10: 
        # repeated action here
        lastTime = time.time()

Otherwise the while loop exits after the argument is false the first time.

UPDATE 2:

Actually, a simpler way to write this is:

 while True:
    # repeated action here
    time.sleep(10)

This also has the benefit that your CPU isn't constantly looping waiting for 10s to pass, freeing up resources for other processes.

Sign up to request clarification or add additional context in comments.

7 Comments

Yeah it isn't automatically looping for some reason
I'll list my changes.
Listed @joshua R.
Updated my answer... look like your timing loop logic needs a little tweak too.
Brilliant! So how does tihs work exactly? What is time.time() ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.