Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jczaplew/6d5cc6d324541cbc8fb4 to your computer and use it in GitHub Desktop.
Save jczaplew/6d5cc6d324541cbc8fb4 to your computer and use it in GitHub Desktop.
Aggregate
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"]])
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