├── fetch_source ├── makejson.py ├── state_fips.json └── README.md /fetch_source: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | curl --create-dirs "https://wonder.cdc.gov/wonder/sci_data/datasets/zipctyA.zip" -o "zipctys/zipctyA.zip" "https://wonder.cdc.gov/wonder/sci_data/datasets/zipctyB.zip" -o "zipctys/zipctyB.zip" 4 | 5 | unzip "zipctys/zipctyA.zip" -d zipctys 6 | unzip "zipctys/zipctyB.zip" -d zipctys 7 | 8 | (echo '1794315,s/ /GA'; echo 'w')|ed -s "zipctys/zipcty4" 9 | -------------------------------------------------------------------------------- /makejson.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import json 4 | import re 5 | 6 | statecodes = json.load(open('state_fips.json')) 7 | zipmap = {} 8 | 9 | for i in range(1,11): 10 | zfile = open('zipctys/zipcty%d' % i) 11 | zfile.readline() # skip first line 12 | for l in zfile: 13 | m = re.match(r"(?P.{5}).{18}(?P..)(?P...)", l) 14 | if m: 15 | r = m.groupdict() 16 | zipmap[r['zip']] = statecodes[r['state']] + r['fips'] 17 | 18 | print(json.dumps(zipmap)) 19 | -------------------------------------------------------------------------------- /state_fips.json: -------------------------------------------------------------------------------- 1 | {"AK":"02", 2 | "AL":"01", 3 | "AR":"05", 4 | "AS":"60", 5 | "AZ":"04", 6 | "CA":"06", 7 | "CO":"08", 8 | "CT":"09", 9 | "DC":"11", 10 | "DE":"10", 11 | "FL":"12", 12 | "FM":"64", 13 | "GA":"13", 14 | "GU":"66", 15 | "HI":"15", 16 | "IA":"19", 17 | "ID":"16", 18 | "IL":"17", 19 | "IN":"18", 20 | "KS":"20", 21 | "KY":"21", 22 | "LA":"22", 23 | "MA":"25", 24 | "MD":"24", 25 | "ME":"23", 26 | "MH":"68", 27 | "MI":"26", 28 | "MN":"27", 29 | "MO":"29", 30 | "MP":"69", 31 | "MS":"28", 32 | "MT":"30", 33 | "NC":"37", 34 | "ND":"38", 35 | "NE":"31", 36 | "NH":"33", 37 | "NJ":"34", 38 | "NM":"35", 39 | "NV":"32", 40 | "NY":"36", 41 | "OH":"39", 42 | "OK":"40", 43 | "OR":"41", 44 | "PA":"42", 45 | "PR":"72", 46 | "PW":"70", 47 | "RI":"44", 48 | "SC":"45", 49 | "SD":"46", 50 | "TN":"47", 51 | "TX":"48", 52 | "UM":"74", 53 | "UT":"49", 54 | "VA":"51", 55 | "VI":"78", 56 | "VT":"50", 57 | "WA":"53", 58 | "WI":"55", 59 | "WV":"54", 60 | "WY":"56"} 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | zip2fips 2 | ======== 3 | 4 | At work, we keep certain location information indexed by 5 | zipcode. Recently, I wanted to feed that data into something that 6 | worked with 5-digit FIPS county codes, and was unable to find an 7 | easily-parsed table that mapped, however roughly, from one to the 8 | other. The best I could find was the CDC's [County Cross 9 | Reference](http://wonder.cdc.gov/wonder/sci_data/codes/fips/type_txt/cntyxref.asp) 10 | files, but I realized I needed to do quite a bit of parsing before 11 | they were really useful to me. I figured it might be of use to someone 12 | else, so here it is. 13 | 14 | The fetch_source script downloads the zip files and makes the one 15 | "cleanup" change required, to line 1794315 of zipcty4, which 16 | originally was missing GA as the state abbreviation. 17 | 18 | makejson.py constructs the json dictionary from zip code to county 19 | code from the zipcty files. The output is in zip2fips.json. 20 | 21 | The state_fips.json file was made by hand, where "by hand" means I 22 | copy-and-pasted [this 23 | table](https://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code#FIPS_state_codes) and 24 | manipulated it in Emacs. 25 | --------------------------------------------------------------------------------