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
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?:
mydictionary={ 'ColumnName1':[dataRow1Col1, dataRow2Col1, dataRow3Col1],
'ColumnName2':[dataRow1Col2, dataRow2Col2, dataRow3Col2],
'ColumnName3':[dataRow1Col3, dataRow2Col3, dataRow3Col3] }
In which case, something like this may work:
import csv
Col1 = "ColumnName1"
Col2 = "ColumnName2"
Col3 = "ColumnName3"
mydictionary={Col1:[], Col2:[], Col3:[]}
csvFile = csv.reader(open("myfile.csv", "rb"))
for row in csvFile:
mydictionary[Col1].append(row[0])
mydictionary[Col2].append(row[1])
mydictionary[Col3].append(row[2])