Skip to content

Commit

Permalink
format in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
GwynHannay committed Oct 22, 2021
1 parent 66acba1 commit aa72d54
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
45 changes: 45 additions & 0 deletions data_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from datetime import datetime

def process_header(header):
header = header.lower()

if header == 'tz':
header = 'timezone'
elif header == 'from':
header = 'tracking_start'
elif header == 'to':
header = 'tracking_end'
elif header == 'sched':
header = 'alarm_scheduled'
elif header == 'hours':
header = 'tracking_hours'

return header

def process_detail(header, detail):

if header in ('tracking_start', 'tracking_end', 'alarm_scheduled'):
detail = datetime.strptime(detail, '%d. %m. %Y %H:%M').strftime('%Y-%m-%d %H:%M')

return detail

def process_event(event):
event_parts = event.split('-')

event_type = event_parts[0]

timestamp = datetime.fromtimestamp(int(event_parts[1])/1000)
event_time = timestamp.strftime('%Y-%m-%d %H:%M')

if len(event_parts) > 2:
event_value = event_parts[2]
else:
event_value = None

event_dict = {
'event_type': event_type,
'event_time': event_time,
'event_value': event_value
}

return event_dict
53 changes: 52 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import csv, json
import csv, json, data_functions as df

# Step 1: Open CSV and read
# Step 2: Select first row
Expand Down Expand Up @@ -51,3 +51,54 @@
# IF type = ALARM_STARTED: event type, timestamp
# IF type = ALARM_DISMISSED: event type, timestamp
# IF field = any other fields?

def conversion(csv_path):
first_pass = []
json_array = []

# read CSV file
with open(csv_path, encoding='utf-8') as csvf:
# load CSV file using csv library's dictionary reader
csv_reader = csv.reader(csvf)

cols = []
# convert each row into Python Dict
for row in csv_reader:
# add this Python Dict to JSON array
if row[0] == 'Id':
headers = []
i = 0
for val in row:
if val == 'Event':
val = val + ' {}'.format(i)
i = i + 1

headers.append(val)
else:
cols = row
zip_it = zip(headers, cols)
dictionary = dict(zip_it)
first_pass.append(dictionary)

i = 0
for record in first_pass:
for key in record:
val = record[key]
#print("{}: {}".format(key, val))

header = df.process_header(key)
print(header)

if header.startswith('event'):
event = df.process_event(val)
print(event)

#field = df.process_detail(header, val)
#print(field)
#break

break

csv_path = r'sleep-as-android/csv/2021-08-10_sleep-export.csv'

conversion(csv_path)

0 comments on commit aa72d54

Please sign in to comment.