使用python创建kml时遇到问题

2024-10-03 13:24:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python和simplekml creator创建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语句,不管我把它们放在什么顺序,它都会为铁山和巴纳比创造一个kml,但不是边栏。这是它返回的结果:

^{pr2}$

错误消息调用what ever if语句在最上面。我被难住了。希望我的问题有意义(为什么它给我这个错误消息,而且不管if语句的顺序是什么,都只创建两个kml)


Tags: andnameinimportforiflinekml
1条回答
网友
1楼 · 发布于 2024-10-03 13:24:36

改变

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

收件人:

^{pr2}$

在第一行中添加

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

为了确保你有足够的台词。。。在

更新

你的回溯(在评论中)显示你的(第一个?!)dayoftheweek似乎是星期三,所以你的前两个ifs被忽略了。那么您的列表a似乎没有任何条目。在

你可以用print("# a: {0}".format(len(a)))来检查这个假设

因此,如果少于3个条目,a[2]==必须失败,因为list index out of range;-)

啊,一开始我没看懂你的问题。如果每一个第一个if语句都抛出一个异常。。。。在

更新2: {Btw>你应该减少循环次数:

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:
        ...

更新3:

你可以“欺骗”一点,如果你要修改一些行,可以做一些类似的事情:

a = line.split(',')
if len(a) < 6:
   continue   # ignores the rest of the loop and jumps stright to the next iteration

相关问题 更多 >