[Tutor] Reading Input Data

jay titleistfour at gmail.com
Tue Jan 15 21:34:15 CET 2008


Python has a cvs module that will make this slightly more elegant

http://docs.python.org/lib/module-csv.html

j

On Jan 15, 2008 2:26 PM, lechtlr <lechtlr at yahoo.com> 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.
>
> I am wondering there is any other easier (and elegant) way of doing this ?
>
> data = []
> for line in open("file.csv"):
>      columns = line.split(',')
>      data.append([columns[1]])
>
> This script returns, say:
> data = [ ['20.0'], ['0.34'], ................,[ 'a:0.20, b:0.30, c:0.50'
> ]]
>
> Then, I assign to a set of variables, say:
>
> x1 = float(data[0][0]) ; x2 = float(data[1][0]);.............;xn =
> data[-1][0]
>
>
> Thanks,
> Lex
>
>
>
>
>
>
> ------------------------------
> Never miss a thing. Make Yahoo your homepage.<http://us.rd.yahoo.com/evt=51438/*http://www.yahoo.com/r/hs>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080115/5692df7a/attachment.htm 


More information about the Tutor mailing list