我正在使用 python 和 simplekml 创建器创建 kml 文件。由于某种原因,它会创建两个 kml 文件,而不会创建第三个。数据对我来说似乎很好。这是代码:
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"
显然在周三晚上对此进行了测试……至于最后三个 if 语句,无论我将它们按什么顺序排列,它都会为 Iron Hill 和 Barnaby's 创建一个 kml,但不会为侧栏创建一个 kml。这是它返回的结果:
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
错误消息指出 if 语句位于顶部。我难住了。希望我的问题是有道理的(为什么它给我这个错误信息,并且无论 if 语句的顺序如何,都只创建两个 kmls)