-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_csv.py
51 lines (44 loc) · 1.57 KB
/
convert_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
46
47
48
49
50
51
import regex
from datetime import datetime
from decimal import *
regex_pattern_position_time = regex.compile(r'\d+\.\d+ -\d+\.\d+ \d+ [a-zA-Z]+')
getcontext().prec = 10
def _line_to_scv(line):
'''
Convert space separated to comman separated
convert from WGS84 format to lat and long
convert unix epoch to datetime
:param line:
:return:
'''
splits = line.split(' ')
splits.append('\n')
# convert from WGS84 format to lat and long
splits[0] = str(Decimal(splits[0]) / Decimal(60))
splits[1] = str(Decimal(splits[1]) / Decimal(60))
# convert unix epoch to datetime
splits[2] = str(datetime.fromtimestamp(int(splits[2])))
return ','.join(splits)
def convert():
'''
Produce such format:
lat,long,time,boat
:return:
'''
with open('data/olexplot', encoding='cp1252') as source:
with open('data/olexplot_prep.csv', 'w') as target:
target.write('lat,long,time,boat,\n')
with open('data/olexplot_meta.csv', 'w') as meta:
meta_line = []
for line in source:
if not line:
continue
line = line.replace('\n', '')
if regex_pattern_position_time.match(line):
if len(meta_line):
meta_line.append('\n')
meta.write(','.join(meta_line))
meta_line.clear()
target.write(_line_to_scv(line))
else:
meta_line.append(line)