python csv到字典列

2024-06-12 17:09:52 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以将csv文件中的数据读入字典,以便列的第一行是键,同一列的其余行构成列表的值?

例如,我有一个csv文件

     strings, numbers, colors 
     string1, 1, blue
     string2, 2, red
     string3, 3, green
     string4, 4, yellow

使用

with open(file,'rU') as f: 
    reader = csv.DictReader(f)
    for row in reader:
        print row

我得到

{'color': 'blue', 'string': 'string1', 'number': '1'}
{'color': 'red', 'string': 'string2', 'number': '2'}
{'color': 'green', 'string': 'string3', 'number': '3'}
{'color': 'yellow', 'string': 'string4', 'number': '4'}

或使用

 with open(file,'rU') as f: 
        reader = csv.reader(f)
        mydict = {rows[0]:rows[1:] for rows in reader}
        print(mydict)

我得到下列词典

{'string3': ['3', 'green'], 'string4': ['4', 'yellow'], 'string2': ['2', 'red'], 'string': ['number', 'color'], 'string1': ['1', 'blue']}

但是,我想

{'strings': ['string1', 'string2', 'string3', 'string4'], 'numbers': [1, 2, 3,4], 'colors': ['red', 'blue', 'green', 'yellow']}

Tags: 文件csvnumberstringgreenblueredreader
3条回答

您需要分析第一行,创建列,然后前进到其余行。

例如:

columns = []
with open(file,'rU') as f: 
    reader = csv.reader(f)
    for row in reader:
        if columns:
            for i, value in enumerate(row):
                columns[i].append(value)
        else:
            # first row
            columns = [[value] for value in row]
# you now have a column-major 2D array of your file.
as_dict = {c[0] : c[1:] for c in columns}
print(as_dict)

输出:

{
    ' numbers': [' 1', ' 2', ' 3', ' 4'], 
    ' colors ': [' blue', ' red', ' green', ' yellow'],
    'strings': ['string1', 'string2', 'string3', 'string4']
}

(一些奇怪的空格,在你的输入“文件”中)。删除逗号前后的空格,如果它们在实际输入中,则使用value.strip()

这就是为什么我们有defaultdict

from collections import defaultdict
from csv import DictReader

columnwise_table = defaultdict(list)
with open(file, 'rU') as f:
    reader = DictReader(f)
    for row in reader:
        for col, dat in row.items():
            columnwise_table[col].append(dat)
print columnwise_table

是的,这是可能的:这样试试:

import csv
from collections import defaultdict

D=defaultdict(list)
csvfile=open('filename.csv')
reader= csv.DictReader(csvfile)  # Dictreader uses the first row as dictionary keys
for l in reader:                 # each row is in the form {k1 : v1, ... kn : vn}
    for k,v in l.items():  
        D[k].append(v)       
...................
...................

假设filename.csv包含一些数据,如

strings,numbers,colors 
string1,1,blue
string2,2,red
string3,3,green
string4,4,yellow

然后D将导致

defaultdict(<class 'list'>, 
            {'numbers': ['1', '2', '3', '4'], 
             'strings': ['string1', 'string2', 'string3', 'string4'], 
             'colors':  ['blue', 'red', 'green', 'yellow']})

相关问题 更多 >