Last active
August 22, 2021 21:35
-
-
Save shahinism/09c637d6f1142e8d0c9b9efcb7b7b066 to your computer and use it in GitHub Desktop.
US State ZipCode Map
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
#!/usr/bin/env python | |
# This is code a quick hack to extract resulted json format from | |
# phaster.com for a project | |
import json | |
import re | |
from collections import defaultdict | |
import requests | |
from pyquery import PyQuery as pq | |
def sanitize_state_string(state): | |
regex = re.compile('(.+)\((.+)\)') | |
match = regex.match(state) | |
try: | |
return match[1].strip(), match[2].strip() | |
except TypeError: | |
return | |
def sanitize_zip_range(zip_str): | |
regex = re.compile('(.+)\s?(thru|-)\s?(.+)') | |
match = regex.match(zip_str) | |
if match: | |
return match[1].strip(), match[3].strip() | |
return [zip_str] | |
def get_table(): | |
res = requests.get('https://www.phaster.com/zip_code.html') | |
html = pq(res.content) | |
rows = [x for x in html('table')[0]] + [x for x in html('table')[1]] | |
table = defaultdict(dict) | |
for row in rows: | |
children = row.getchildren() | |
state_str = children[0].text_content() | |
san_state = sanitize_state_string(state_str) | |
if not san_state: | |
continue | |
state, code = san_state | |
city = children[1].text_content().split('\u00a0') | |
zip_code = children[2].text_content().split('\u00a0') | |
table[state] = {'state_code': code, 'map': {}} | |
for i, c in enumerate(city): | |
table[state]['map'][c] = sanitize_zip_range(zip_code[i]) | |
return table | |
table = get_table() | |
with open('result.json', 'w') as fp: | |
json.dump(table, fp, indent=True) |
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
{ | |
"Alabama": { | |
"state_code": "AL", | |
"map": { | |
"Huntsville": [ | |
"35801", | |
"35816" | |
] | |
} | |
}, | |
"Alaska": { | |
"state_code": "AK", | |
"map": { | |
"Anchorage": [ | |
"99501", | |
"99524" | |
] | |
} | |
}, | |
"Arizona": { | |
"state_code": "AZ", | |
"map": { | |
"Phoenix": [ | |
"85001", | |
"85055" | |
] | |
} | |
}, | |
"Arkansas": { | |
"state_code": "AR", | |
"map": { | |
"Little Rock": [ | |
"72201", | |
"72217" | |
] | |
} | |
}, | |
"California": { | |
"state_code": "CA", | |
"map": { | |
"Sacramento": [ | |
"94203", | |
"94209" | |
], | |
"Los Angeles": [ | |
"90001", | |
"90089" | |
], | |
"Beverly Hills": [ | |
"90209", | |
"90213" | |
] | |
} | |
}, | |
"Colorado": { | |
"state_code": "CO", | |
"map": { | |
"Denver": [ | |
"80201", | |
"80239" | |
] | |
} | |
}, | |
"Conneticut": { | |
"state_code": "CT", | |
"map": { | |
"Hartford": [ | |
"06101", | |
"06112" | |
] | |
} | |
}, | |
"Deleware": { | |
"state_code": "DE", | |
"map": { | |
"Dover": [ | |
"19901", | |
"19905" | |
] | |
} | |
}, | |
"District of Columbia": { | |
"state_code": "DC", | |
"map": { | |
"Washington": [ | |
"20001", | |
"20020" | |
] | |
} | |
}, | |
"Florida": { | |
"state_code": "FL", | |
"map": { | |
"Pensacola": [ | |
"32501", | |
"32509" | |
], | |
"Miami": [ | |
"33124", | |
"33190" | |
], | |
"Orlando": [ | |
"32801", | |
"32837" | |
] | |
} | |
}, | |
"Georgia": { | |
"state_code": "GA", | |
"map": { | |
"Atlanta": [ | |
"30301", | |
"30381" | |
] | |
} | |
}, | |
"Hawaii": { | |
"state_code": "HI", | |
"map": { | |
"Honolulu": [ | |
"96801", | |
"96830" | |
] | |
} | |
}, | |
"Idaho": { | |
"state_code": "ID", | |
"map": { | |
"Montpelier": [ | |
"83254" | |
] | |
} | |
}, | |
"Illinois": { | |
"state_code": "IL", | |
"map": { | |
"Chicago": [ | |
"60601", | |
"60641" | |
], | |
"Springfield": [ | |
"62701", | |
"62709" | |
] | |
} | |
}, | |
"Indiana": { | |
"state_code": "IN", | |
"map": { | |
"Indianapolis": [ | |
"46201", | |
"46209" | |
] | |
} | |
}, | |
"Iowa": { | |
"state_code": "IA", | |
"map": { | |
"Davenport": [ | |
"52801", | |
"52809" | |
], | |
"Des Moines": [ | |
"50301", | |
"50323" | |
] | |
} | |
}, | |
"Kansas": { | |
"state_code": "KS", | |
"map": { | |
"Wichita": [ | |
"67201", | |
"67221" | |
] | |
} | |
}, | |
"Kentucky": { | |
"state_code": "KY", | |
"map": { | |
"Hazard": [ | |
"41701", | |
"41702" | |
] | |
} | |
}, | |
"Lousiana": { | |
"state_code": "LA", | |
"map": { | |
"New Orleans": [ | |
"70112", | |
"70119" | |
] | |
} | |
}, | |
"Maine": { | |
"state_code": "ME", | |
"map": { | |
"Freeport": [ | |
"04032", | |
"04034" | |
] | |
} | |
}, | |
"Maryland": { | |
"state_code": "MD", | |
"map": { | |
"Baltimore": [ | |
"21201", | |
"21237" | |
] | |
} | |
}, | |
"Massachusetts": { | |
"state_code": "MA", | |
"map": { | |
"Boston": [ | |
"02101", | |
"02137" | |
] | |
} | |
}, | |
"Michigan": { | |
"state_code": "MI", | |
"map": { | |
"Coldwater": [ | |
"49036" | |
], | |
"Gaylord": [ | |
"49734", | |
"49735" | |
] | |
} | |
}, | |
"Minnesota": { | |
"state_code": "MN", | |
"map": { | |
"Duluth": [ | |
"55801", | |
"55808" | |
] | |
} | |
}, | |
"Mississippo": { | |
"state_code": "MS", | |
"map": { | |
"Biloxi": [ | |
"39530", | |
"39535" | |
] | |
} | |
}, | |
"Missouri": { | |
"state_code": "MO", | |
"map": { | |
"St. Louis": [ | |
"63101", | |
"63141" | |
] | |
} | |
}, | |
"Montana": { | |
"state_code": "MT", | |
"map": { | |
"Laurel": [ | |
"59044" | |
] | |
} | |
}, | |
"Nebraska": { | |
"state_code": "NE", | |
"map": { | |
"Hastings": [ | |
"68901", | |
"68902" | |
] | |
} | |
}, | |
"Nevada": { | |
"state_code": "NV", | |
"map": { | |
"Reno": [ | |
"89501", | |
"89513" | |
] | |
} | |
}, | |
"New Hampshire": { | |
"state_code": "NH", | |
"map": { | |
"Ashland": [ | |
"03217" | |
] | |
} | |
}, | |
"New Jersey": { | |
"state_code": "NJ", | |
"map": { | |
"Livingston": [ | |
"07039" | |
] | |
} | |
}, | |
"New Mexico": { | |
"state_code": "NM", | |
"map": { | |
"Santa Fe": [ | |
"87500", | |
"87506" | |
] | |
} | |
}, | |
"New York": { | |
"state_code": "NY", | |
"map": { | |
"New York": [ | |
"10001", | |
"10048" | |
] | |
} | |
}, | |
"North Carolina": { | |
"state_code": "NC", | |
"map": { | |
"Oxford": [ | |
"27565" | |
] | |
} | |
}, | |
"North Dakota": { | |
"state_code": "ND", | |
"map": { | |
"Walhalla": [ | |
"58282" | |
] | |
} | |
}, | |
"Ohio": { | |
"state_code": "OH", | |
"map": { | |
"Cleveland": [ | |
"44101", | |
"44179" | |
] | |
} | |
}, | |
"Oklahoma": { | |
"state_code": "OK", | |
"map": { | |
"Tulsa": [ | |
"74101", | |
"74110" | |
] | |
} | |
}, | |
"Oregon": { | |
"state_code": "OR", | |
"map": { | |
"Portland": [ | |
"97201", | |
"97225" | |
] | |
} | |
}, | |
"Pennsylvania": { | |
"state_code": "PA", | |
"map": { | |
"Pittsburgh": [ | |
"15201", | |
"15244" | |
] | |
} | |
}, | |
"Rhode Island": { | |
"state_code": "RI", | |
"map": { | |
"Newport": [ | |
"02840", | |
"02841" | |
] | |
} | |
}, | |
"South Carolina": { | |
"state_code": "SC", | |
"map": { | |
"Camden": [ | |
"29020" | |
] | |
} | |
}, | |
"South Dakota": { | |
"state_code": "SD", | |
"map": { | |
"Aberdeen": [ | |
"57401", | |
"57402" | |
] | |
} | |
}, | |
"Tennessee": { | |
"state_code": "TN", | |
"map": { | |
"Nashville": [ | |
"37201", | |
"37222" | |
] | |
} | |
}, | |
"Texas": { | |
"state_code": "TX", | |
"map": { | |
"Austin": [ | |
"78701", | |
"78705" | |
] | |
} | |
}, | |
"Utah": { | |
"state_code": "UT", | |
"map": { | |
"Logan": [ | |
"84321", | |
"84323" | |
] | |
} | |
}, | |
"Vermont": { | |
"state_code": "VT", | |
"map": { | |
"Killington": [ | |
"05751" | |
] | |
} | |
}, | |
"Virginia": { | |
"state_code": "VA", | |
"map": { | |
"Altavista": [ | |
"24517" | |
] | |
} | |
}, | |
"Washington": { | |
"state_code": "WA", | |
"map": { | |
"Bellevue (home of windoze)": [ | |
"98004", | |
"98009" | |
] | |
} | |
}, | |
"West Virginia": { | |
"state_code": "WV", | |
"map": { | |
"Beaver": [ | |
"25813" | |
] | |
} | |
}, | |
"Wisconsin": { | |
"state_code": "WI", | |
"map": { | |
"Milwaukee": [ | |
"53201", | |
"53228" | |
] | |
} | |
}, | |
"Wyoming": { | |
"state_code": "WY", | |
"map": { | |
"Pinedale": [ | |
"82941" | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment