How can I turn a csv file into a list of list in python

12,736

Solution 1

In case you sure it's fixed number of items in each row, you can use zip:

import csv

with open('test.csv') as csvfile:
    rows = csv.reader(csvfile)
    res = list(zip(*rows))
    print(res)
    # [('6', '5', '7'), ('2', '2', '3'), ('4', '3', '6')]

Or in case it's different number of items in row:

6,2,4
5,2
7

Use zip_longest and filter:

import csv
from itertools import zip_longest

with open('test.txt') as csvfile:
    rows = csv.reader(csvfile)

    res = list(zip_longest(*rows))
    print(res)
    # [('6', '5', '7'), ('2', '2', None), ('4', None, None)]

    res2 = [list(filter(None.__ne__, l)) for l in res]
    print(res2)
    # [['6', '5', '7'], ['2', '2'], ['4']]

Solution 2

You could probably start by reading it into a list of lists first:

from csv import reader as csvreader
with open(input, 'r') as fp:
    reader = csvreader(fp)
    li = list(reader)

Then chop it into a new sequence, I'm sure there are other tricks with itertools but this is what I came up with:

from itertools import count
def my_gen():
    for i in count():
        try:
            yield [x[i] for x in li]
        except IndexError:
            break

You can now turn the generator into a list, which will have the desired columns as rows.

list(my_gen())
Share:
12,736
Kyuu
Author by

Kyuu

Updated on June 18, 2022

Comments

  • Kyuu
    Kyuu almost 2 years

    I want to be able to turn csv file into a list of lists with the column values for each list. For example:

    6,2,4
    5,2,3
    7,3,6
    

    into

    [[6,5,7],[2,2,3],[4,3,6]]
    

    Ive only managed to open the file and only having success printing it as rows

    with open(input,'rb') as csvfile:
            csv_file = csv.reader(csvfile)
    
            header = csv_file.next() 
    
            raw_data = csv_file
    
  • izak
    izak about 8 years
    or izip from itertools if you're using python2.x, and avoid turning it into a list unless you really NEED a list :-)
  • izak
    izak about 8 years
    @germn's answer is better than mine. zip/izip essentially does what my my_gen does.