Reputation: 677
I'm trying to append multiple columns of a csv to multiple lists. Column 1 will go in list 1, column 2 will go in list 2 etc...
However I want to be able to not hard code in the number of columns so it could work with multiple csv files. So I've used a column count to assign how many lists there should be.
I'm coming unstuck when trying to append values to these lists though. I've initiated a count that should be able to assign the right column to the right list however it seems like the loop just exits after the first loop and wont append the other columns to the list.
import csv
#open csv
f = open('attendees1.csv')
csv_f = csv.reader(f)
#count columns
first_row = next(csv_f)
num_cols = len(first_row)
#create multiple lists (within lists) based on column count
d = [[] for x in xrange(num_cols)]
#initiate count
count = 0
#im trying to state that whilst the count is less than the amount of columns, rows should be appended to lists, which list and which column will be defined by the [count] value.
while count < (num_cols):
for row in csv_f:
d[count].append(row[count])
count += 1
print count
print d
Upvotes: 0
Views: 92
Reputation: 4548
The iteration for row in csv_f:
does not reset after each instance of the while
loop, thus this loop exits immediately after the first time through.
You can read in everything as a list of rows, then transpose it to create a list of columns:
import csv
with open('attendees1.csv', 'r') as f:
csv_f = csv.reader(f)
first_row = next(csv_f) # Throw away the first row
d = [row for row in csv_f]
d = zip(*d)
See Transpose a matrix in Python.
If you want to keep re-reading the CSV file in the same manner as the OP, you can do that as well (but this is extremely inefficient):
while count < (num_cols):
for row in csv_f:
d[count].append(row[count])
count += 1
print count
f.seek(0) # rewind to the beginning of the file
next(csv_f) # throw away the first line again
See Python csv.reader: How do I return to the top of the file?.
Upvotes: 3
Reputation: 756
Transposing the list of rows is a very elegant answer. There is another solution, not so elegant, but a little more transparent for a beginner. Read rows, and append each element to the corresponding list, like so:
for row in csv_f:
for i in range(len(d)):
d[i].append(row[i])
Upvotes: 1