""" for each capitalized city and state, goes to USPS site and gets the first zipcode use? I did this as part of learning Python. """ import urllib import string # conversion table from USPS for STATE -> 2 digit abbreviation us_states = { "ALABAMA":"AL" ,"ALASKA":"AK" ,"AMERICAN SAMOA":"AS" ,"ARIZONA":"AZ" ,"ARKANSAS":"AR" ,"CALIFORNIA":"CA" ,"COLORADO":"CO" ,"CONNECTICUT":"CT" ,"DELAWARE":"DE" ,"DISTRICT OF COLUMBIA":"DC" ,"FEDERATED STATES OF MICRONESIA":"FM" ,"FLORIDA":"FL" ,"GEORGIA":"GA" ,"GUAM":"GU" ,"HAWAII":"HI" ,"IDAHO":"ID" ,"ILLINOIS":"IL" ,"INDIANA":"IN" ,"IOWA":"IA" ,"KANSAS":"KS" ,"KENTUCKY":"KY" ,"LOUISIANA":"LA" ,"MAINE":"ME" ,"MARSHALL ISLANDS":"MH" ,"MARYLAND":"MD" ,"MASSACHUSETTS":"MA" ,"MICHIGAN":"MI" ,"MINNESOTA":"MN" ,"MISSISSIPPI":"MS" ,"MISSOURI":"MO" ,"MONTANA":"MT" ,"NEBRASKA":"NE" ,"NEVADA":"NV" ,"NEW HAMPSHIRE":"NH" ,"NEW JERSEY":"NJ" ,"NEW MEXICO":"NM" ,"NEW YORK":"NY" ,"NORTH CAROLINA":"NC" ,"NORTH DAKOTA":"ND" ,"NORTHERN MARIANA ISLANDS":"MP" ,"OHIO":"OH" ,"OKLAHOMA":"OK" ,"OREGON":"OR" ,"PALAU":"PW" ,"PENNSYLVANIA":"PA" ,"PUERTO RICO":"PR" ,"RHODE ISLAND":"RI" ,"SOUTH CAROLINA":"SC" ,"SOUTH DAKOTA":"SD" ,"TENNESSEE":"TN" ,"TEXAS":"TX" ,"UTAH":"UT" ,"VERMONT":"VT" ,"VIRGIN ISLANDS":"VI" ,"VIRGINIA":"VA" ,"WASHINGTON":"WA" ,"WEST VIRGINIA":"WV" ,"WISCONSIN":"WI" ,"WYOMING":"WY" } # core function. Give it a city in caps and 2 letter statecode, returns first zipcode def getzipcode(city, stateco): '''gets the first zipcode''' params = urllib.urlencode({'ctystzip':city + ' ' + stateco}) f = urllib.urlopen('http://www.usps.gov/cgi-bin/zip4/ctystzip2',params) for line in f.readlines(): if string.find(line, '<b>' + city + ' ' + stateco)>=0: s=string.find(line,'------<BR><BR>') s = s + len('------<BR><BR>') return string.strip(line[s:(s+6)]) f.close() if __name__ == '__main__': #test the function with some data # your data table - STATE, CITY and ZIPCODE (this will probably be null) us_cities = [ ("ALABAMA","MONTGOMERY","36101") ,("ALABAMA","SELMA","36701") ,("ALASKA","ANCHORAGE","99501") ,("ARIZONA","PHOENIX","85001") ,("ARIZONA","TUSCON","") ,("ARIZONA","WICKENBERG","") ,("ARKANSAS","FAYETTEVILLE","") ,("ARKANSAS","LITTLE ROCK","") ,("CALIFORNIA","SAN FRANCISCO","") ,("COLORADO","BOULDER","") ,("COLORADO","MOSCA","") ,("CONNECTICUT","ESSEX","") ,("CONNECTICUT","MIDDLETOWN","") ,("CONNECTICUT","NEW MILFORD","") ,("CONNECTICUT","WESTON","") ,("FLORIDA","ALTAMONTE SPRINGS","") ,("FLORIDA","APALACHICOLA OR OTHER","") ,("FLORIDA","BRISTOL","") ,("FLORIDA","JUPITER ISLAND","") ,("FLORIDA","MIAMI","") ,("FLORIDA","TBD","") ,("FLORIDA","WEST PALM BEACH","") ,("GEORGIA","ATLANTA","") ,("GEORGIA","DARIEN","") ,("GEORGIA","SAVANNAH","") ,("GEORGIA","THOMASVILLE","") ,("HAWAII","HONLULU","") ,("HAWAII","HONOLULU","") ,("ILLINOIS","CARY","") ,("ILLINOIS","CHICAGO","") ,("IOWA","DES MOINES","") ,("LOUISIANA","BATON ROUGE","") ,("MAINE","BRUNSWICK","") ,("MAINE","WASHINGTON COUNTY","") ,("MASSACHUSETTS","BOSTON","") ,("MASSACHUSETTS","VINEYARD HAVEN","") ,("MICHIGAN","GRAND RAPIDS","") ,("MICHIGAN","MARQUETTE","") ,("MICHIGAN","OWOSSO","") ,("MINNESOTA","KARLSTAD","") ,("MINNESOTA","MINNEAPOLIS","") ,("MINNESOTA","WABASHA","") ,("MISSISSIPPI","CAMP SHELBY","") ,("MISSOURI","ST. LOUIS","") ,("NEVADA","MINDEN","") ,("NEW HAMPSHIRE","CONCORD","") ,("NEW JERSEY","CHESTER","") ,("NEW YORK","ADIRONDACKS/KEENE VALLEY","") ,("NEW YORK","COLD SPRING HARBOR","") ,("NEW YORK","KEENE VALLEY","") ,("NEW YORK","LONG ISLAND","") ,("NEW YORK","LONG ISLAND CITY","") ,("NEW YORK","TROY","") ,("NORTH CAROLINA","DURHAM","") ,("OHIO","DUBLIN","") ,("OKLAHOMA","NEAR PAWHUSKA","") ,("OKLAHOMA","TULSA","") ,("PENNSYLVANIA","CONSHOHOCKEN","") ,("PENNSYLVANIA","HARRISBURG","") ,("PENNSYLVANIA","MEADVILLE","") ,("RHODE ISLAND","PROVIDENCE","") ,("SOUTH CAROLINA","COLUMBIA","") ,("TENNESSEE","NASHVILLE","") ,("TEXAS","CANYON","") ,("TEXAS","CORPUS CHRISTI","") ,("TEXAS","MATAGORDA COUNTY","") ,("TEXAS","MISSION","") ,("TEXAS","SAN ANTONIO","") ,("TEXAS","SILSBEE","") ,("TEXAS","TEXAS CITY","") ,("VERMONT","MANCHESTER","") ,("VERMONT","MONTPELIER","") ,("VERMONT","WEST HAVEN","") ,("VIRGINIA","ARLINGTON","") ,("WYOMING","JACKSON","") ] for us_city in us_cities: try: (state,city,zipcode) = us_city if us_states.has_key(state): print (city,state,getzipcode(city,us_states[state])) except: print "Error: ",us_city
Since you are seeing this, it means that your browser does not support cascading style sheets. Please download and use one of the many browsers that support web standards.