I am attempting to write to append values to a csv. I can search for and find where there are missing fields, but want to know if there is a way to insert 0 as a default value for these fields.
I have the following code.
def fillBlanks():
HEADERS = ['ST','Year','PCT_SHORT','PCT_V_SHORT','Year','PCT_SHORT',
'PCT_V_SHORT','Year','PCT_SHORT','PCT_V_SHORT' ]
fileH = open(outputDir+"PCT_SHORT_V_SHORT.csv", 'rb')
reader = csv.DictReader(fileH, HEADERS)
for row in reader:
if any(row[key] in (None, "") for key in row):
print "bad"+ str(row)
fileH.close()
This gives me the rows and columns that are missing as follows.
bad{'PCT_SHORT': None, 'Year': None, 'PCT_V_SHORT': None, 'ST': 'NV'}
bad{'PCT_SHORT': None, 'Year': None, 'PCT_V_SHORT': None, 'ST': 'CA'}
bad{'PCT_SHORT': None, 'Year': None, 'PCT_V_SHORT': None, 'ST': 'AZ'}
bad{'PCT_SHORT': None, 'Year': None, 'PCT_V_SHORT': None, 'ST': 'US'}
Is there a way to code in default values so no fields are missing? I would like to make the fields either 999 or 0. I hope this is clear, I am new to python.
EDIT: Here is a sample of the data ('OK', '2015', '14', '3', '2014', '28', '17', '2013', '19', '17', '2012', '36', '12') ('AZ', '2015', '14', '2', '2014', '36', '2') ('ID', '2015', '12', '0', '2014', '28', '4', '2013', '24', '2', '2012', '14', '1')