-
Notifications
You must be signed in to change notification settings - Fork 0
/
kml_to_csv.py
45 lines (35 loc) · 1.41 KB
/
kml_to_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from bs4 import BeautifulSoup
inputfile = "my_places1.kml"
with open(inputfile, 'r') as f:
soup = BeautifulSoup(f)
# After you have a soup object, you can access tags very easily.
# For instance, you can iterate over and get <description> like so:
name = []
coords = ['']
datetime = ['']
joined = []
for node in soup.select('name'):
node1 = str(node).strip(',')
name.append(str(node1))
for node in soup.select('coordinates'):
coords.append(str(node))
for node in soup.select('TimeStamp'):
datetime.append(str(node))
for i in range(len(name)):
namestr = name[i].replace('<name>', '')
namestr = namestr.replace('</name>', '')
namestr = namestr.replace('<![CDATA[', '')
namestr = namestr.replace(']]>', '')
namestr = namestr.strip(',')
datestr = datetime[i].replace('<TimeStamp><when>', '')
datestr = datestr.replace('</when></timestamp>', '')
datestr = datestr.replace('<timestamp><when>', '')
datestr = datestr[0:10]
coordstr = coords[i].replace('<Point>', '')
coordstr = coordstr.replace('</Point>', '')
coordstr = coordstr.replace('<coordinates>', '')
coordstr = coordstr.replace('</coordinates>', '')
joined.append(namestr + ' ' + datestr + ', ' + coordstr)
for i in joined:
with open('my_places.csv', 'a') as f:
f.write(i + '\n')