Created
July 2, 2015 20:50
-
-
Save dniku/8c9850f5dfeb0fbb4a3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import pandas as pd | |
import datetime | |
time_format = '%d. %m. %Y %-H:%M' | |
def from_javatime(time): | |
return datetime.datetime.fromtimestamp(time // 1000) | |
def quote(s): | |
return '"%s"' % s | |
def prepare_note(note): | |
note = note.replace('\n', ' \\n ') | |
note = note.replace('"', '""') | |
return quote(note) | |
df = pd.read_csv('hours.csv') | |
df = df[df['deleted'] == 0] | |
df = df[df['sleep'] < df['awake']] | |
df.fillna('', inplace=True) | |
df.sort(columns='sleep', ascending=False, inplace=True) | |
with open('extracted_ids.txt', 'r') as f: | |
ids = f.readlines() | |
with open('sleepbot-converted.csv', 'w') as f: | |
for i, row in enumerate(df.iterrows()): | |
row = row[1] # why | |
time_from = from_javatime(row['sleep']) | |
time_to = from_javatime(row['awake']) | |
delta = (time_from - from_javatime(int(ids[i]))).total_seconds() | |
hours = (time_to - time_from).total_seconds() / 3600 | |
rating = row['rating'] | |
if rating == -1: | |
rating = 0 | |
f.write(','.join([ | |
'Id', | |
'Tz', | |
'From', | |
'To', | |
'Sched', | |
'Hours', | |
'Rating', | |
'Comment', | |
'Framerate', | |
'Snore', | |
'Noise', | |
'Cycles', | |
'DeepSleep', | |
'LenAdjust', | |
'Geo', | |
quote(time_to.strftime('%-H:%M')), | |
'"Event"', | |
'"Event"']) + '\n') | |
f.write(','.join([ | |
quote(ids[i].strip()), # quote(row['sleep'] // 10000 * 10000), | |
'"Europe/Moscow"', | |
quote(time_from.strftime(time_format)), | |
quote(time_to.strftime(time_format)), | |
quote(time_to.strftime(time_format)), | |
quote('%.3f' % hours), | |
quote('%.1f' % rating), | |
prepare_note(row['note']), | |
'"10000","-1","-1.0","-1","-1.0","0","","0.0"', | |
'"DEEP_START-%d"' % row['sleep'], | |
'"DEEP_END-%d"' % row['awake']]) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment