简体   繁体   中英

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]

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

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

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 ie

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

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']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM