Last active
August 29, 2015 14:10
-
-
Save jczaplew/6d5cc6d324541cbc8fb4 to your computer and use it in GitHub Desktop.
Aggregate
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 | |
lookup = {} | |
with open('CPCs.csv', 'rb') as input: | |
clinics = csv.reader(input) | |
for clinic in clinics: | |
key = clinic[1].replace(' ', '') + clinic[2] | |
if key not in lookup: | |
lookup[key] = {"clinics": 0, "city": clinic[1], "state": clinic[2]} | |
lookup[key]["clinics"] += 1 | |
with open('CPCs_grouped.csv', 'w') as output: | |
writer = csv.writer(output, delimiter=',') | |
writer.writerow(["city", "state", "clinics"]) | |
for clinic in lookup: | |
writer.writerow([lookup[clinic]["city"], lookup[clinic]["state"], lookup[clinic]["clinics"]]) | |
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 json | |
# Dictionary to keep track of unique city/state combos | |
lookup = {} | |
# Open up the CSV | |
with open('CPCs.csv', 'rb') as input: | |
clinics = csv.reader(input) | |
# For each row in the file... | |
for clinic in clinics: | |
# Create a unique key (city with spaces removed + state - ex: GreenBayWI) | |
key = clinic[1].replace(' ', '') + clinic[2] | |
# If we haven't seen this city/state combo before, record it | |
if key not in lookup: | |
lookup[key] = {"clinics": []} | |
# Add this clinic to the appropriate city/state combo | |
lookup[key]["clinics"].append({'name': clinic[0], 'src': clinic[3]}) | |
# Write the output to a JSON file | |
with open('CPCs_grouped.json', 'w') as output: | |
json.dump(lookup, output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment