Postal Code Zips and Location (Python)

vegaseat 2 Tallied Votes 4K Views Share

Using http://download.geonames.org/export/zip/ you can get postal information for most countries in the world. The raw data string can be transformed into a Python list of lists that can be searched for zip codes, locations, longitude and latitude coordinates.

''' postal_codes101.py

exploring the postal code zip files

from:
http://download.geonames.org/export/zip/
download eg.  US.zip
then unzip to US.txt
for instance to folder C:/Zz5/PostalCodes

The data format is tab-delimited text (utf8 encoded), with the following 12 fields:

country code      : iso country code, 2 characters
postal code       : varchar(20)
place name        : varchar(180)
admin name1       : 1. order subdivision (state) varchar(100)
admin code1       : 1. order subdivision (state) varchar(20)
admin name2       : 2. order subdivision (county/province) varchar(100)
admin code2       : 2. order subdivision (county/province) varchar(20)
admin name3       : 3. order subdivision (community) varchar(100)
admin code3       : 3. order subdivision (community) varchar(20)
latitude          : estimated latitude (wgs84)
longitude         : estimated longitude (wgs84)
accuracy          : accuracy of lat/lng from 1=estimated to 6=centroid

Note:
latitude N(+90) to S(-90), equator = 0
longitude W(-180) to E(+180), prime meridian (Greenwich UK) = 0

For instance this would be one line in the text:
US	82730	Upton	Wyoming	WY	Weston	045			44.0893	-104.6352

tested with Python27 and Python34  by  vegaseat   19jan2015
'''

# modify path if necessary
fname = "C:/Zz5/PostalCodes/US.txt"
with open(fname) as fin:
    data_str = fin.read()

# create a list of lists
data_list = []
for line in data_str.split('\n'):
    mylist = line.split('\t')
    if len(mylist) > 11:
        data_list.append(mylist)

#print(data_list[700])  # test

# search information by zip code
myzip = '48105'
for sublist in data_list:
    zip_code = sublist[1]
    if zip_code == myzip:
        #print(sublist) # test
        location = "{}, {}  {}".format(sublist[2], sublist[3], myzip)
        county = sublist[5]
        latitude = sublist[9]
        longitude = sublist[10]
        print(location)
        print("county/province = {}".format(county))
        print("latitude  = {}".format(latitude))
        print("longitude = {}".format(longitude))

print('-'*40)

# search zip code(s) by city and state
city = "Reno"
state = "Nevada"
for sublist in data_list:
    city_from_list = sublist[2]
    state_from_list = sublist[3]
    # shows all the zip codes for this location
    if city == city_from_list and state == state_from_list:
        zip_code = sublist[1]
        print("{}, {} has zip code {}".format(city, state, zip_code))

''' result ...
Ann Arbor, Michigan  48105
county/province = Washtenaw
latitude  = 42.3042
longitude = -83.7068
----------------------------------------
Reno, Nevada has zip code 89501
Reno, Nevada has zip code 89502
Reno, Nevada has zip code 89503
Reno, Nevada has zip code 89504
Reno, Nevada has zip code 89505
Reno, Nevada has zip code 89506
Reno, Nevada has zip code 89507
Reno, Nevada has zip code 89508
Reno, Nevada has zip code 89509
Reno, Nevada has zip code 89510
Reno, Nevada has zip code 89511
Reno, Nevada has zip code 89512
Reno, Nevada has zip code 89513
Reno, Nevada has zip code 89515
Reno, Nevada has zip code 89519
Reno, Nevada has zip code 89520
Reno, Nevada has zip code 89521
Reno, Nevada has zip code 89523
Reno, Nevada has zip code 89533
Reno, Nevada has zip code 89550
Reno, Nevada has zip code 89555
Reno, Nevada has zip code 89557
Reno, Nevada has zip code 89564
Reno, Nevada has zip code 89570
Reno, Nevada has zip code 89595
Reno, Nevada has zip code 89599
'''