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?:
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])
for row in thingthatyieldslists:
col1, col2, col3 = row
print "%s: %s, %s" % (col1, col2, col3)
Is this what you were looking for (Python 3.X):
import csv
from io import StringIO
# Simulate a csv data file with no header
data = StringIO('''\
Mark,44,1
Joe,22,0
Craig,39,3
''')
for row in csv.DictReader(data,'Name Age Children'.split()):
print(row)
{'Age': '44', 'Name': 'Mark', 'Children': '1'}
{'Age': '22', 'Name': 'Joe', 'Children': '0'}
{'Age': '39', 'Name': 'Craig', 'Children': '3'}
Or maybe:
import csv
from io import StringIO
# Simulate a csv data file with no header
data = StringIO('''\
Mark,44,1
Joe,22,0
Craig,39,3
''')
# 1. Read in all the data
# 2. Transpose into columns
# 3. Match with column names
# 4. Create dictionary
cols = dict(zip('Name Age Children'.split(),zip(*csv.reader(data))))
print(cols)
{'Age': ('44', '22', '39'), 'Name': ('Mark', 'Joe', 'Craig'), 'Children': ('1', '0', '3')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With