Last active
November 14, 2020 03:51
-
-
Save 7kry/776a1bb02dfebd7b8e4f47ba5cbfedb8 to your computer and use it in GitHub Desktop.
This file contains 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 os | |
import os.path | |
import shutil | |
import sys | |
srcdir = sys.argv[1] | |
os.chdir(srcdir) | |
with open('activities.csv') as csvfile: | |
r = csv.reader(csvfile) | |
columns = next(r) | |
rows = list(dict(zip(columns, row)) for row in r) | |
for row in rows: | |
bike, filename = row['Activity Gear'], row['Filename'] | |
if os.path.exists(filename) and os.path.dirname(filename) == 'activities': | |
new_filename = os.path.join('activities', bike, os.path.basename(filename)) | |
row['Filename'] = new_filename | |
new_filename = os.path.join('activities', bike, os.path.basename(filename)) | |
if not os.path.exists(os.path.dirname(new_filename)): | |
os.mkdir(os.path.dirname(new_filename)) | |
shutil.move(filename, new_filename) | |
shutil.move('activities.csv', 'activities.csv.org') | |
with open('activities.csv', 'w') as csvfile: | |
w = csv.writer(csvfile) | |
w.writerow(columns) | |
for r in rows: | |
w.writerow([r[c] for c in columns]) |
This file contains 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 | |
from collections import defaultdict | |
import os | |
import os.path | |
import pprint | |
import shutil | |
import sys | |
srcdir = sys.argv[1] | |
os.chdir(srcdir) | |
bikes = defaultdict(lambda: 0) | |
with open('activities.csv') as csvfile: | |
r = csv.reader(csvfile) | |
columns = next(r) | |
rows = list(dict(zip(columns, row)) for row in r) | |
for row in rows: | |
dist = float(row['Distance']) | |
bikes[row['Activity Gear']] += dist | |
pprint.pprint(bikes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment