as a noob, I'm struggling with converting a column of numbers in a CSV to a float.
the test file looks like this: test.csv = [['a','1'],['b','2']].
My code:
def readLines():
def conv(s):
try:
s=float(s)
except ValueError:
pass
return s
with open('C:/Users/Public/Documents/Scripts/test.csv', 'rU') as data:
reader = csv.reader(data)
for row in reader:
for cell in row:
y=conv(cell)
print (y)
readLines()
The result is this:
a
1.0
b
2.0
But I really just want to have the array modified so that the number are floats, and everything else is kept intact.
Also, it would be nicer from my perspective to keep the code in order - ie open the file, then covert.
yis not part of the row.