I want to read a CSV file’s columns directly into variables. The result should be something like you would get with the following shell line:
while IFS=, read ColumnName1 ColumnName2 ColumnName3
do stuff
So far the answer seems to be with csv.DictReader, but I have not been able to make it work. I do not have a header row, so column names would have to be created manually. (with a dictionary I think in the form mydictionary={ ‘ColumnName1’:0, ‘ColumnName2’:1, ‘ColumnName3’:3 } )
Also, can the columns be referenced as simple variable names or must you use a list[index] style reference. A code sample just printing the columns by name would be nice. Thanks for the help.
The built-in CSV Module is quite useful when working with csv files.
Oh, nevermind, you must be using it already, if you are looking at DictReader.
The usual way I deal with files that have no header would be to read the first line, parse it for the number of commas (and hence the number of columns) then set up my dictionary/list to contain the values from the csv file (using number of columns and giving each column a name in my my code.) I can provide an example if necessary, it’s pretty straightforward.
I think I better understand your question, is this more what you are looking for?:
In which case, something like this may work: