alig227
alig227

Reputation: 357

Read all columns from CSV file?

I am trying to read in a CSV file and then take all values from each column and put into a separate list. I do not want the values by row. Since the CSV reader only allows to loop through the file once, I am using the seek() method to go back to the beginning and read the next column. Besides using a Dict mapping, is there a better way to do this?

infile = open(fpath, "r")
reader = csv.reader(infile)    

NOUNS = [col[0] for col in reader]
infile.seek(0)  # <-- set the iterator to beginning of the input file

VERBS = [col[1] for col in reader]
infile.seek(0)
ADJECTIVES = [col[2] for col in reader]
infile.seek(0) 
SENTENCES = [col[3] for col in reader]

Upvotes: 1

Views: 2301

Answers (4)

nbui
nbui

Reputation: 192

This works assuming you know exactly how many columns are in the csv (and there isn't a header row).

NOUNS = []
VERBS = []
ADJECTIVES = []
SENTENCES = []
with open(fpath, "r") as infile:
    reader = csv.reader(infile)    

    for row in reader:
        NOUNS.append(row[0])
        VERBS.append(row[1])
        ADJECTIVES.append(row[2])
        SENTENCES.append(row[3])

If you don't know the column headers, you're going to have to be clever and read off the first row, make lists for every column you encounter, and loop through every new row and insert in the appropriate list. You'll probably need to do a list of lists.

If you don't mind adding a dependency, use Pandas. Use a DataFrame and the method read_csv(). Access each column using the column name i.e.

df = pandas.DataFrame.read_csv(fpath)
print df['NOUN']
print df['VERBS']

Upvotes: 1

martineau
martineau

Reputation: 123501

Something like this would do it in one pass:

kinds = NOUNS, VERBS, ADJECTIVES, SENTENCES = [], [], [], []
with open(fpath, "r") as infile:
    for cols in csv.reader(infile):
        for i, kind in enumerate(kinds):
            kind.append(cols[i])

Upvotes: 1

saurabh baid
saurabh baid

Reputation: 1877

I am not sure why you dont want to use dict mapping. This is what I end up doing

Data

col1,col2,col3
val1,val2,val3
val4,val5,val6

Code

import csv
d = dict()
with open("abc.text") as csv_file:
    reader =  csv.DictReader(csv_file)
    for row in reader:
        for key, value in row.items():
            if d.get(key) is None:
                d[key] = [value]
            else:
                d[key].append(value)

print d
{'col2': ['val2', 'val5'], 'col3': ['val3', 'val6'], 'col1': ['val1', 'val4']}

Upvotes: 0

niemmi
niemmi

Reputation: 17263

You could feed the reader to zip and unpack it to variables as you wish.

import csv

with open('input.csv') as f:
    first, second, third, fourth = zip(*csv.reader(f))
    print('first: {}, second: {}, third: {}, fourth: {}'.format(
        first, second, third, fourth
    ))

With following input:

1,2,3,4
A,B,C,D

It will produce output:

first: ('1', 'A'), second: ('2', 'B'), third: ('3', 'C'), fourth: ('4', 'D')

Upvotes: 1

Related Questions