Question

I am creating kml files using python and the simplekml creator. For some reason it creates two kml files and will not create the third. Data seems fine to me. Here is the code:

times=open("E:\\Python\Copyofresttable.csv","r")
import time
import csv
import simplekml
from time import strftime, localtime
dayoftheweekvariable = strftime("%A", localtime())
print dayoftheweekvariable
kml = simplekml.Kml()


if dayoftheweekvariable == "Monday":
     for line in times:
        a = line.split(',')
        if a[2] == "Monday":
            print a[5]

if dayoftheweekvariable == "Tuesday":
     for line in times:
        a = line.split(',')
        if a[2] == "Tuesday":
            print a[5]

if dayoftheweekvariable == "Wednesday":
    for line in times:
        a = line.split(',')

        if a[1]== "Iron Hill" and a[2] =="Wednesday":
            kml.newpoint(name="Iron Hill", coords=[(-75.605507,39.960504)], description=a[5])
            kml.save("pythonmap.kml")
            print "Creating KML"

        if a[1]== "Side Bar and Resturant" and a[2] =="Wednesday":
            kml.newpoint(name="Side Bar and Resturant", coords=[(-75.604805,39.960591)], description=a[5])
            kml.save("pythonmap.kml")
            print "Creating KML"

        if a[1]== "Barnaby's" and a[2] =="Wednesday":
            kml.newpoint(name="Barnaby's", coords=[(-75.604049,39.959488)], description=a[5])
            kml.save("pythonmap.kml")
            print "Creating KML"

obviously testing this out on Wednesday night...As for the last three if statements, no matter what order i put them in, it will create a kml for Iron Hill and Barnaby's but not side bar. this is the result it returns:

Wednesday
Creating KML
Creating KML

Traceback (most recent call last):
  File "C:/Users/75IV740906/Desktop/py117", line 26, in <module>
    if a[1]== "Iron Hill" and a[2] =="Wednesday":
IndexError: list index out of range

the error message calls out what ever if statement is on top. Im stumped. Hope my question makes sense(why is it giving me this error message and only creating two kmls no matter what order the if statements are in)

No correct solution

OTHER TIPS

Change

times=open("E:\\Python\Copyofresttable.csv","r")

to:

times=open("E:\\Python\Copyofresttable.csv","r").read()

in your first line and add a

print('#Times: {0}'.format(len(times.split())))

to make sure you have enough lines...

Update:

You traceback (in comment) shows that your (1st?!) dayoftheweek seems to be a wednesday, that's why your 1st two ifs are ignored. Then it seems like your list a doesn't have nough entries.

You may check that assumption with a print("# a: {0}".format(len(a)))

So if you have less then 3 entries a[2]== must fail because the list index out of range ;-)

Ahh, i didn't read your question properly at first. And it makes more sense that way, if every 1st if-statement is throwing an exception....

Update 2: Btw: You should rearange your for line in times: loops to a less redundant way, like:

lines=open("E:\\Python\Copyofresttable.csv","r").readlines()
...
for line in lines:
    a = line.split(',')
    if a[2] == "Monday" == dayoftheweek:
        ...
    elif a[2] == "Tuesday" == dayoftheweek:
        ...
    elif a[1]== "Iron Hill" and a[2] =="Wednesday" == dayoftheweek:
        ...

Update 3:

You could "cheat" a little if ommiting some lines, by doing something like:

a = line.split(',')
if len(a) < 6:
   continue   # ignores the rest of the loop and jumps stright to the next iteration
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top