I am trying to read a file using csv.DictReader
I have a field that is supposed to be an integer.if it is empty i will set it as DEFAULT
if it is an integer i do nothing.if it is not an integer i check if it is a quoted integer(like '1234').if so I will convert it to integer.Else an exception is raised.
it works as expected if its empty or it is an integer.Now if it is a quoted integer(like '1234') ,an exception is raised integer invalid literal for float(): '1234'.
it is not supposed to raise exception in this case.
I think it has something to do with the way DictReader reads the csv file. Everyhting else is just fine.Please help
if not line[key]:
line[key]='DEFAULT'
elif not isinstance(line[key], (int, long, float)) :
try:
line[key]=float(line[key])
except Exception,e :
print e
float()should work just fine on strings. However, if all the numbers are integers, why not useint().