Skip to content

Instantly share code, notes, and snippets.

@ianlewis
Last active December 14, 2016 00:31
Show Gist options
  • Save ianlewis/88e43ca8c9a4d01ac11c to your computer and use it in GitHub Desktop.
Save ianlewis/88e43ca8c9a4d01ac11c to your computer and use it in GitHub Desktop.
A conversion script for SleepBot CSV data. Merges multiple rows on for the same date.
#:coding=utf-8:
import sys
import csv
import collections
import decimal
import datetime
def main(csv_path, output_path):
with open(csv_path) as csv_file:
reader = csv.reader(csv_file)
# ヘッダーをスキップ
reader.next()
dates = collections.defaultdict(lambda: 0)
for line in reader:
date = datetime.datetime.strptime(line[0], "%m/%d/%y")
hours = line[3]
dates[date] += decimal.Decimal(hours)
with open(output_path, "wb") as outfile:
writer = csv.writer(outfile)
for date, hours in sorted(dates.items(), reverse=True):
writer.writerow([date.strftime("%Y/%m/%d"), hours])
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment