在Python中从csv创建kml [英] Create kml from csv in Python

查看:806
本文介绍了在Python中从csv创建kml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手。我正在处理gps文件。我需要将所有gps数据的CSV文件转换为kml文件。下面是我使用的python中的代码:

  import csv 
#输入文件名。
fname = raw_input(输入文件名无扩展名:)
data = csv.reader(open(fname +'.csv'),delimiter =',')
#Skip第一个标题行。
data.next()
#打开要写入的文件。
f = open('csv2kml.kml','w')

#打开kml文件。
f.write(
f.write(< kml xmlns ='http: //earth.google.com/kml/2.1'>\\\

f.write(< Document> \\\

f.write(< name& + fname +'.kml'+< / name> \\\

对于数据中的行:
f.write(< Placemark> \\\

f.write(< name>+ str(row [1])+< / name> \\\

f.write 0])+< / description> \\\

f.write(< Point> \\\

f.write(< coordinate> row [3])+,+ str(row [2])+,+ str(row [4])+< / coordinate> \\\

f.write < / Point> \ n)
f.write(< / Placemark> \\\

f.write(< / Document> $ b f.write(< / kml> \\\

print文件已创建。
print按ENTER键退出。
raw_input()

我使用的csv文件位于: dip12Sep11newEdited.csv
生成的kml文件位于此处: csv2kml.kml
但是没有正确创建kml文件。显然,在csv中的一些行后,代码无法生成更多的地标。它不能迭代。你可以看到,通过滚动到生成的kml文件的最后一部分。



任何人都可以帮助我找出代码中的错误,因为对于一些较小的csv文件

解决方案

你没有回答上面的查询,但我的猜测是,错误是你不关闭你的输出文件(这将刷新你的输出)。

  f.close()

$ b b

I am new to Python. I am working on gps files. I need to convert a CSV file having all the gps data to kml file. Below is the code in python I am using :

import csv
#Input the file name.
fname = raw_input("Enter file name WITHOUT extension: ")
data = csv.reader(open(fname + '.csv'), delimiter = ',')
#Skip the 1st header row.
data.next()
#Open the file to be written.
f = open('csv2kml.kml', 'w')

#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")
f.write("<Document>\n")
f.write("   <name>" + fname + '.kml' +"</name>\n")
for row in data:
    f.write("   <Placemark>\n")
    f.write("       <name>" + str(row[1]) + "</name>\n")
    f.write("       <description>" + str(row[0]) + "</description>\n")
    f.write("       <Point>\n")
    f.write("           <coordinates>" + str(row[3]) + "," + str(row[2]) + "," + str(row[4]) + "</coordinates>\n")
    f.write("       </Point>\n")
    f.write("   </Placemark>\n")
f.write("</Document>\n")
f.write("</kml>\n")
print "File Created. "
print "Press ENTER to exit. "
raw_input()

The csv file I am using is available here : dip12Sep11newEdited.csv The kml file generated is available here : csv2kml.kml But the kml file is not getting created correctly. Apparently after some rows in the csv the code is not able to generate more Placemarks. Its not able to iterate. You can see that by scrolling to the last part of the kml file generated.

Can anyone help me finding out the error in the code, because for some smaller csv files it worked correctly and created kml files fully.

Thanks.

解决方案

You didn't answer the query above, but my guess is that the error is that you're not closing your output file (which would flush your output).

f.close()

这篇关于在Python中从csv创建kml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆