Looping Through Nested List - Convert from String to Float
Question: I am new to Python, so apologies if this seems extremely simple:

I have a csv file with 4 columns and a dozen rows. I import the contests as a
list (list of lists), and the contents come in as strings. What I want to do
is loop through the list (which can be a variable number of rows) and convert
the index 2 string to a float. I run the following code, but when i print the
list, everything is still a string:

    
    
    import csv
    
    def main():
        benchmark = list(csv.reader(open('test.csv', 'r')))
    
        for i in range((len(benchmark))):
            benchmark[i][2] = float(benchmark[i][2])
    

Any help would be much appreciated.

Thanks!


Answer: It's not necessary to cast your csv.reader to a list as it is already
iterable. You can do something like the following:

    
    
    benchmark=[]
    with open('test.csv','r') as inp:
        csvin=csv.reader(inp)
        for row in csvin:
            benchmark.append(row[:1] + [float(row[1])] + row[2:])
    

