Q:

Converting some CSV elements to float Python

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.

A:

I am not sure, what would be the result you desire, but this is how I understand it: You want to read data from a CSV-file into a python data structure with the second cell in each line converted to a float:
with open('C:/Users/Public/Documents/Scripts/test.csv', 'rU') as data:
    reader = csv.reader(data)
    # using list comprehension
    result = [(row[0], float(row[1])) for row in reader]

print result

Out:
[('a', 1.0), ('b', 2.0)]

