[Tutor] Input into lists?

Dave Angel davea at davea.name
Fri Aug 16 02:28:47 CEST 2013


leam hall wrote:

> I'm trying to take input from a file (CSV spreadsheet) and use the first
> line inputs (header) as the names of lists. So far I'm not successful.   :)
>
> How do I take line_list[0], the result of "line.split(',')" and use it as
> the name of a list? Does that make sense?
>

No, it doesn't make sense.  You're trying to define a variable that has
a name determined not by your source, but by a name in line 0 of the csv
file.  How then are you going to use that variable?

There are two possibilities:  Either you actually know those names and
are using them in your code, or the names won't be known till runtime.

If the latter, then make a dictionary of lists,

mydict = {}
for item in line_list:
    mydict[item] = []

and then populate those lists from each line of the file.

Now, if you're sure you know what the names are to be, then:

names = mydict["name"]
addresses = mydict["address"]

or whatever.




-- 
DaveA




More information about the Tutor mailing list