""" parseZip.py Parses a district to zip code mapping and outputs a properly formatted txt file Source data: http://www2.census.gov/geo/relfiles/cdsld13/natl/natl_zccd_delim.txt Author: Matt Vukas (mvukas@indiana.edu) """ from pprint import pprint DEBUG = False # dictionary mapping state postal codes to the state abbreviation STATE_DICT = {1:"AL", 2:"AK", 4:"AZ", 5:"AR", 6:"CA", 8:"CO", 9:"CT", 10:"DE", 11:"DC", 12:"FL", 13:"GA", 15:"HI", 16:"ID", 17:"IL", 18:"IN", 19:"IA", 20:"KS", 21:"KY", 22:"LA", 23:"ME", 24:"MD", 25:"MA", 26:"MI", 27:"MN", 28:"MS", 29:"MO", 30:"MT", 31:"NE", 32:"NV", 33:"NH", 34:"NJ", 35:"NM", 36:"NY", 37:"NC", 38:"ND", 39:"OH", 40:"OK", 41:"OR", 42:"PA", 72:"PR", 44:"RI", 45:"SC", 46:"SD", 47:"TN", 48:"TX", 49:"UT", 50:"VT", 51:"VA", 53:"WA", 54:"WV", 55:"WI", 56:"WY"} # input file is renamed to district-to-zip.txt for clarity with open("district-to-zip.txt") as infile: with open("zip4dist-prefix.txt", "w+") as newfile: lineCount = 0 for line in infile: if lineCount > 1: lineData = line.split(",") stateCode = STATE_DICT[int(lineData[0])] zipCode = lineData[1] districtCode = lineData[2].strip() outputLine = zipCode + " " + stateCode + "-" + districtCode + "\n" newfile.write(outputLine) if DEBUG: pprint(lineData) print outputLine lineCount += 1