-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdriving.py
executable file
·48 lines (40 loc) · 1.47 KB
/
driving.py
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
#!/usr/bin/env python
import argparse
from collections import defaultdict
import csv
import datetime
import sys
ingest_date_format = '%B %d, %Y at %I:%M%p'
output_date_format = '%Y-%W'
def output(weeks, out):
out.write('"Mileage","Honda Fit"\n')
for d in sorted(weeks):
out.write('{0}, {1}\n'.format(d, weeks[d]))
out.write('"Colors","Red"\n')
# out.write('"Totals"\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Summarize trips to weekly miles')
parser.add_argument('input', help='CSV trip filename')
parser.add_argument('-o', '--output', help='filename for weekly mileage CSV output')
args = parser.parse_args()
weeks = defaultdict(float)
try:
with open(args.input, 'rt') as csv_file:
csv_reader = csv.reader(csv_file)
for row_number, row in enumerate(csv_reader):
if len(row) < 11: continue
try:
trip_start = row[0]
trip_mileage = row[10]
dt = datetime.datetime.strptime(trip_start, ingest_date_format)
day = dt.strftime(output_date_format)
weeks[day] += float(trip_mileage)
except:
print 'error parsing line %d' % (row_number + 1, )
except IOError:
pass
if args.output is None:
output(weeks, sys.stdout)
else:
with open(args.output, 'wt') as out:
output(weeks, out)