Skip to content

Instantly share code, notes, and snippets.

@edavis
Created December 22, 2014 17:16
Show Gist options
  • Save edavis/0b2178bc66dbe2a1c606 to your computer and use it in GitHub Desktop.
Save edavis/0b2178bc66dbe2a1c606 to your computer and use it in GitHub Desktop.
Produce a hierarchical text file of ACS concepts and variables
#!/usr/bin/env python
import requests
from collections import defaultdict
r = requests.get('http://api.census.gov/data/2013/acs5/variables.json')
acs = r.json()['variables']
c = defaultdict(list)
for key, value in acs.iteritems():
concept = value['concept']
label = value['label']
c[concept].append((key, label))
with open('/Users/eric/Desktop/acs.txt', 'w') as fp:
for concept in sorted(c.keys()):
fp.write('%s\n' % concept)
for key, value in sorted(c[concept]):
if 'MARGIN OF ERROR' in value.upper(): continue
indent = value.count('!!') + 1
if indent > 1:
value = value.rsplit('!!', 1)[-1]
fp.write(('\t' * indent) + '%s: %s\n' % (key, value))
fp.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment