从CSV文件中读取所有列?

时间:2016-09-25 03:54:15

标签: python python-3.x csv list-comprehension

我正在尝试读取CSV文件,然后从每列中获取所有值并将其放入单独的列表中。我不想按行排列值。由于CSV阅读器只允许循环遍历文件一次,因此我使用seek()方法返回到开头并阅读下一列。除了使用Dict映射外,还有更好的方法吗?

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]

4 个答案:

答案 0 :(得分:1)

您可以将reader提供给zip并根据需要将其解压缩到变量。

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

使用以下输入:

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

它将产生输出:

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

答案 1 :(得分:1)

像这样的东西可以一次性完成:

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

答案 2 :(得分:1)

这可以假设您确切知道csv中有多少列(并且没有标题行)。

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

如果您不知道列标题,那么您必须要聪明并从第一行读取,为您遇到的每一列创建列表,并遍历每个新行并插入适当的清单。您可能需要列出一份清单。

如果您不介意添加依赖项,请使用Pandas。使用DataFrame和方法read_csv()。使用列名访问每列,即

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

答案 3 :(得分:0)

我不确定你为什么不想使用dict映射。这就是我最终要做的事情

数据

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

<强>代码

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