我正在尝试从 csv 构建一个 KML 文件以在 Google Earth 中显示点,但我遇到了一些小问题......首先,我可以让它构建文件,但 csv 有六组坐标在其中,kml 只显示两个(实际上是一个显示两次)。我显然弄错了循环,但不知道怎么做。其次,生成的地标 ['53.66018'],['-1.94925'] 显示在某处的海洋中,当它应该在曼彻斯特(英国)时,我将不胜感激。提前致谢!这是Python代码-
import csv
import os
fp = "C:\\Python27\\test.csv"
file = open(fp)
lines =file.readlines()
for line in lines:
line = line.strip()
fields = line.split(';') #semicolon seperated
LCID = fields[0].split() #splitting
CID = fields[1].split()
LAC = fields[2].split()
NET = fields[3].split()
LAT = fields[4].split()
LON = fields[5].split()
print 'LCID' #print splitted
print ("CID: " + str(CID))
# print ("LAC: " + str(LAC))
# print ("NET: " + str(NET))
# print ("LAT: " + str(LAT))
# print ("LON: " + str(LON))
f = open('C:\\Python27\\csv17kml.kml', 'w')
fname = "testing_Actions"
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://www.opengis.net/kml/2.2'>\n")
f.write("<Folder>\n")
f.write(" <name> Op Example </name>\n")
for row in lines:
f.write(" <Placemark>\n")
f.write(" <ExtendedData>\n")
f.write(" <Data name='CID'>\n")
f.write(" <value>\n")
f.write(" " + str(CID) +"\n")
f.write(" </value>\n")
f.write(" </Data>\n")
f.write(" <Data name='LAC'>\n")
f.write(" <value>\n")
f.write(" " + str(LAC) + "\n")
f.write(" </value>\n")
f.write(" </Data>\n")
f.write(" </ExtendedData>\n")
f.write(" <Point>\n")
f.write(" <coordinates>" + str(LAT) + "," + str(LON) + "</coordinates>\n")
f.write(" </Point>\n")
f.write(" </Placemark>\n")
f.write("</Folder>\n")
f.write("</kml>\n")
print ("File Created. ")
f.close
file.close()
这是csv中的数据:
LCID;CID;LAC;NET;LAT;LON
26599;26599;3569;23410;53.66018;-1.94925;
26599;26599;3569;23410;53.66018;-1.94923;
26599;26599;3569;23410;53.66019;-1.94922;
26599;26599;3569;23410;53.66019;-1.94919;
26599;26599;3569;23410;53.66018;-1.94919;
26599;26599;3569;23410;53.66018;-1.94917
这是我添加的页眉和页脚部分...
def write_header(output_file):
return
"""<?xml version='1.0' encoding='UTF-8'?>\n
<kml xmlns='http://www.opengis.net/kml/2.2'>\n
<Folder>\n
<name> Op Example </name>\n"""
def write_footer(output_file):
return
"""</Folder>\n")
</kml>\n
print ("File Created. ")
f.close
file.close()"""