[Tutor] Reading Input Data

bob gailer bgailer at alum.rpi.edu
Tue Jan 15 22:31:06 CET 2008

lechtlr wrote:
> I want to read an input file (file.csv) that has two columns. I want 
> to read 2nd column and assign variables that are strings and floats. 
> Currently, I use the following split() function to read from the input 
> file and create a list, and then assign each element to a variable.
In addition to Jay (it's csv module not cvs) and Michael's responses I add:

It is rarely a good idea to assign a bunch of variables (x1...xn).

What's the reason you're doing this?

You can just create another list holding the floated values. If you 
assign this list to x then you can refer to x[0], x[1], ...x[n].

Also you have created lists within your list, to no apparent purpose;


x = [float(line.split(',')[1]) for line in open("file.csv")]


More information about the Tutor mailing list