0

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
saph_top
  • 677
  • 1
  • 6
  • 23

2 Answers2

3

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?.

Community
  • 1
  • 1
PaSTE
  • 4,050
  • 18
  • 26
  • 1
    This is a much nicer answer than I was just formulating based on the existing code, upvote. Can you modify your answer to use `with` to open the file as it's currently left open in OP code? The whole program can be done in 4 lines of code. – roganjosh May 18 '16 at 18:13
  • will read up on this thank you! is there any reason why the iteration does not rest after each instance of the while loop? is there any way to not make it exit? – saph_top May 18 '16 at 18:40
  • 1
    @saph_top: Iteration though the CSV file is done as the file is read off the disk. It is done this way to avoid having to read the entire file into memory. Resetting after each iteration of the `while` loop would require rewinding the file back to the beginning to start over. – PaSTE May 18 '16 at 19:22
1

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])