Created
June 26, 2014 20:28
-
-
Save vilterp/772a1e5f0b51050e3eaf 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
| import csv | |
| import datetime | |
| import time | |
| import pprint | |
| import itertools | |
| def get_data(): | |
| f = open('DATA_1334.csv') | |
| reader = csv.DictReader(f.read().splitlines()) | |
| rows = [] | |
| for row in reader: | |
| # extract row | |
| utc = row['utc'] | |
| # parse into datetime | |
| format_withtime = '%m/%d/%y %H:%M' | |
| format_no_time = '%m/%d/%y' | |
| try: | |
| dt_struct = time.strptime(utc, format_withtime) | |
| except ValueError: | |
| dt_struct = time.strptime(utc, format_no_time) | |
| dt = datetime.datetime.fromtimestamp(time.mktime(dt_struct)) | |
| rows.append({ | |
| 'datetime': dt, | |
| 'use': row['use'] | |
| }) | |
| return rows | |
| def group_by_date(rows): | |
| return itertools.groupby(rows, key=lambda row: row['datetime'].date()) | |
| grouped = group_by_date(get_data()) | |
| def sum_groups(groups): | |
| summed = [] | |
| for date, group in groups: | |
| sum = 0 | |
| for item in group: | |
| sum += float(item['use']) | |
| summed.append({ | |
| 'date': date, | |
| 'use_sum': round(sum, 3) | |
| }) | |
| return summed | |
| sums = sum_groups(group_by_date(get_data())) | |
| output = open('summed_1334.csv', 'w') | |
| writer = csv.DictWriter(output, ['date', 'use_sum']) | |
| writer.writeheader() | |
| writer.writerows(sums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment