A line in the online data table looks like this:
"06","91436","CA","ENCINO",118.488238,34.15098,13605,0.000457
If we split each line apart at the commas, we will end up with eight fields. Since Python counts from zero, this means the zip code is field number 1. The longitude and latitude are fields 4 and 5 respectively. Our script reads in the data file a line at a time, then creates a dictionary so we can look up each zip code and get a string representing the latitude and longitude. (For Google Maps, we want latitude first, and longitude expressed as a negative number.)
datafile = open('zips.txt', 'r')
lookup = {}
for line in datafile.readlines():
items = line.split(',')
zip = items[1]
lat = items[5]
lon = items[4]
lookup[zip[1:-1]] = "%s, -%s" % (lat, lon)
We now can look up individual zip codes:
print lookup['91436']
returns
'34.15098, -118.488238'
Or we can transform all of our data at once:
ziplist = ['02139', '02141', '02142', ...]
print [lookup[z] for z in ziplist]
returns
['42.364688, -71.104155', '42.370701, -71.088277', '42.362025, -71.083011', ...]
Tags: application program interface | geocoding | Google | munging | python