Skip to content
Browse files

switched to using csv for the zip code file

  • Loading branch information...
1 parent e854f13 commit 6323757ff46269379dd135ecc02c3c8af65e9d40 @neilkod committed Oct 6, 2012
Showing with 6,789 additions and 17 deletions.
  1. +18 −17 county_fips_codes_for_zip_codes.py
  2. +6,771 −0 output/zip_code_to_fips_county_code.txt
View
35 county_fips_codes_for_zip_codes.py
@@ -3,13 +3,13 @@
# http://quickfacts.census.gov/qfd/download/FIPS_CountyName.txt
# http://www.unitedstateszipcodes.org/zip_code_database.csv
-import sys, re
+import sys, re, csv
counties = {}
no_match = []
zip_fips = {}
fips_regex = re.compile('^[0-9]{5}')
fips_data = open('data/FIPS_CountyName.txt').readlines()
-zip_data = open('data/zip_code_database.csv').readlines()
+zip_data = 'data/zip_code_database.csv'
output_file = open('output/zip_code_to_fips_county_code.txt','w')
for line in fips_data:
@@ -32,22 +32,23 @@
# - convert it to upper case
# - remove periods (i.e. ST.)
# - remove the surrounding double-quotes
-
-for line in zip_data:
- data = line.strip().split(',')
- zip_code = data[0].replace('"','')
- state_cd = data[5].replace('"','').upper()
- if len(state_cd) != 2:
- continue
- county_name = data[6].replace('"','').upper().replace('.','')
+with open(zip_data, 'rb') as f:
+ reader = csv.reader(f)
+ for data in reader:
+ zip_code = data[0]
+ state_cd = data[5].upper()
+ print zip_code, state_cd
+ if len(state_cd) != 2:
+ continue
+ county_name = data[6].replace('"','').upper().replace('.','')
- try:
- fips_cd_for_zip_code = counties[(state_cd, county_name)]
- zip_fips[zip_code] = fips_cd_for_zip_code
- output_file.write("%s\t%s\t%s\t%s\n" %
- (zip_code, fips_cd_for_zip_code, county_name, state_cd))
- except KeyError:
- no_match.append((state_cd, county_name))
+ try:
+ fips_cd_for_zip_code = counties[(state_cd, county_name)]
+ zip_fips[zip_code] = fips_cd_for_zip_code
+ output_file.write("%s\t%s\t%s\t%s\n" %
+ (zip_code, fips_cd_for_zip_code, county_name, state_cd))
+ except KeyError:
+ no_match.append((state_cd, county_name))
output_file.close()
print "found county matches for %d zip codes" % len(zip_fips.items())
print "-------"
View
6,771 output/zip_code_to_fips_county_code.txt
6,771 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit 6323757

Please sign in to comment.
Something went wrong with that request. Please try again.