from bs4 import BeautifulSoup
import csv
infile = '1.kml'
outfile = '1.csv'
with open(infile, 'r') as f:
s = BeautifulSoup(f, 'xml')
with open(outfile, 'wb') as csvfile:
writer = csv.writer(csvfile)
for coords in s.find_all('coordinates'):
# Take coordinate string from KML and break it up into [Lat,Lon,Lat,Lon...] to get CSV row
space_splits = coords.string.split(" ")
# print(space_splits)
row = []
headers = ['lon','lat']
writer.writerow(headers)
for split in space_splits[0:]:
# Note: because of the space between <coordinates>" "-80.123, we slice [1:]
comma_split = split.split(',')
print(comma_split)
if(len(split.split(',')) == 3):
# lattitude
row.append(comma_split[1])
# longitude
row.append(comma_split[0])
rows = [(comma_split[0],comma_split[1])]
writer.writerows(rows)
读取Google earth地图输出的kml中的经纬高保存到csv文件
最新推荐文章于 2024-10-05 19:33:25 发布