7

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
Kyuu
  • 953
  • 6
  • 15
  • 25
  • 2
    http://stackoverflow.com/questions/16503560/read-specific-columns-from-csv-file-with-python-csv – msvalkon Mar 24 '16 at 07:35
  • import csv aList =[] with open('/Users/YYY/Desktop/Workbook1.csv', 'rU') as f: reader = csv.reader(f) for row in reader: aList.append( row ) print aList # [['6', '2', '4'], ['5', '2', '3'], ['7', '3', '6']] – Ark Mar 24 '16 at 07:43
  • output needs to be arranged by columns so [[6,5,7],[2,2,3],[4,3,6]] – Kyuu Mar 24 '16 at 07:45

3 Answers3

12

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']]
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • 1
    or izip from itertools if you're using python2.x, and avoid turning it into a list unless you really NEED a list :-) – izak Mar 24 '16 at 07:53
3

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())
izak
  • 981
  • 7
  • 10
  • 1
    @germn's answer is better than mine. zip/izip essentially does what my my_gen does. – izak Mar 24 '16 at 07:55
0

Or maybe like this...

from csv import reader   

with open('test.csv') as csv_file:
    csv_reader = reader(csv_file)
    rows = list(csv_reader)
    print(rows)
Harley
  • 1,305
  • 1
  • 13
  • 28