2

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)

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75

1 Answers1

0

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
Don Question
  • 11,227
  • 5
  • 36
  • 54
  • This is what I got with your edits:#Times: 1048 Wednesday Traceback (most recent call last): File "C:/Users/75IV740906/Desktop/py117", line 28, in if a[1]== "Limoncello" and a[2] =="Wednesday": IndexError: list index out of range – user1807770 Nov 08 '12 at 01:18
  • Didnt create any kmls this time, but there seem to be enough lines – user1807770 Nov 08 '12 at 01:19
  • lines are enough, but are there enough records in every line ... i'll elaborate in an updated answer – Don Question Nov 08 '12 at 01:20
  • There seem to be, like i said the error message will change to which ever if statement is on top, thanks for the help by the way – user1807770 Nov 08 '12 at 01:22
  • Result:Wednesday # a: 6 # a: 7 # a: 6 # a: 6 Creating KML # a: 6 # a: 6 # a: 6 # a: 6 # a: 6 # a: 6 # a: 6 Creating KML # a: 6 # a: 1 – user1807770 Nov 08 '12 at 01:28
  • you see: your last a is `1` ! That's where your index a[n] for n>0 must fail! – Don Question Nov 08 '12 at 01:30
  • Does it mean that there is only one column in the last a? – user1807770 Nov 08 '12 at 01:33
  • exactly! i can't tell you why ;-) – Don Question Nov 08 '12 at 01:34
  • okay, the csv file isnt showing me what python is telling me. thanks for the help though – user1807770 Nov 08 '12 at 01:35
  • But that makes no sense, if I switch the order of the if statements, I get the same result – user1807770 Nov 08 '12 at 01:37
  • yes it does. because you iterate over ALL a's in EVERY if statement, that's why i proposed another execution-flow. – Don Question Nov 08 '12 at 01:49
  • Sorry to be a bug but I am still not sure how to fix this, tried to implement your third update but I got a bunch of new errors – user1807770 Nov 08 '12 at 02:05
  • Maybe it's time to review your data-set? `map(print, open("E:\\Python\Copyofresttable.csv","r").readlines().split(','))` to get a quick overview of all datalines, if your data-set isn't to big. i would invite you to a chat, but you don't have enough reputation-points yet for that! :-( – Don Question Nov 08 '12 at 02:10