Permalink
Browse files

Update projects/fdic/save_to_csv.py

Changed delimiter back to pipe; added specific error-trapping.
  • Loading branch information...
1 parent 9e539c7 commit be9a9d3270a44cbe910b3f8fafd89ea799ae66cf @cschnaars cschnaars committed Feb 22, 2013
Showing with 5 additions and 7 deletions.
  1. +5 −7 projects/fdic/save_to_csv.py
@@ -25,7 +25,8 @@ def convertdatestring(datestring):
try:
dt = datetime.strptime(datestring, '%B %d, %Y')
ret_date = dt.strftime('%Y-%m-%d')
- except:
+ except ValueError:
+ print("Can't convert %s to date. Setting to NULL." % datestring)
pass
return ret_date
@@ -61,15 +62,13 @@ def convertdatestring(datestring):
# Let's mess up one row to demo try/except:
# data[0]['closed'] = 'Jnauary 15, 2013'
-# Each dictionary has these keys
-# bank, city, state, cert_num, acq_inst, closed, updated, url
-
# Iterate through each row of our data and verify data types valid
for row in data:
# First, we'll verify cert_num is an integer
try:
row['cert_num'] = int(row['cert_num'])
- except:
+ except ValueError:
+ print("%s is not a valid integer. Setting to zero." % row['cert_num'])
row['cert_num'] = 0
# Now we'll look at the two date fields. This is a little more
@@ -86,12 +85,11 @@ def convertdatestring(datestring):
row['updated'] = ''
with open(savedir + 'fdic_output.txt', 'w') as outputfile:
- wtr = csv.DictWriter(outputfile, delimiter=',', fieldnames=outputheaders,
+ wtr = csv.DictWriter(outputfile, delimiter='|', fieldnames=outputheaders,
lineterminator='\n', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
# Add headers to output
wtr.writeheader()
# Write the data
wtr.writerows(data)
-

0 comments on commit be9a9d3

Please sign in to comment.