-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser_2.py
More file actions
66 lines (51 loc) · 1.64 KB
/
test_parser_2.py
File metadata and controls
66 lines (51 loc) · 1.64 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import csv
import json
import collections
from collections import defaultdict
"""
"YYYY/MM/DD": {
"sleeptimes": "",
"comments": "",
"glucose": [
{"time": "21:53", "value": 22.92},
{"time": "17:57", "value": 19.59},
{"time": "12:12", "value": 19.26},
{"time": "08:06", "value": 5.72}
]
}
"""
filename = r"C:\Users\zeffi\OneDrive\Documents\Export_4212016.csv"
some_dict = defaultdict(list)
def sanedate(date):
MM, DD, YYYY = date.split('/')
return '/'.join([YYYY, MM, DD])
def formatted_time(gtime):
HH, MM, SS = gtime.split(':')
return ':'.join([HH, MM])
def open_csv_test(filename):
#csvfile = open(filename, 'r', encoding='ISO-8859-15', newline='')
csvfile = open(filename, 'r', newline='')
ofile = csv.reader(csvfile, delimiter=',')
# skip the first 7 lines (OneTouch uses an odd csv format)
for i in range(6):
next(ofile)
for row in ofile:
try:
print(row)
date, gtime, gvalue = row[1:4]
date = sanedate(date)
gtime = formatted_time(gtime)
some_dict[date].append({'time': gtime, 'value': float(gvalue)})
except:
print("failed at")
print(row)
for k, v in some_dict.items():
v = v.reverse()
new_some_dict = defaultdict(dict)
for k, v in some_dict.items():
new_some_dict[k]['glucose'] = v
new_some_dict[k].update(dict(times="", comments=""))
with open('C:/Users/zeffi/Documents/some_constructed.json', 'w') as wfile:
wfile.write(json.dumps(new_some_dict, sort_keys=True, indent=4))
open_csv_test(filename)