将csv中的标头添加到字典键以及它们下面的列作为值列表

时间:2016-07-29 11:22:29

标签: python python-3.x csv

我有以下csv文件:

h1  h2  h3  h4
10  11  12  13
14  15  16  17
18  19  20  21

我想要获得的输出是字典:

dict = {'h1': ['10','14','18'], 'h2': ['11','15','19'], 
        'h3': ['12','16','20'], 'h4': ['13','17','21']}

我尝试了以下但我没有得到我需要的东西:

import csv
from collections import defaultdict

def get_columns_from_source_file():
    source_file_reader = csv.DictReader(open('custom_delimited_file'))
    columns_storage = defaultdict(list)

    for source_file_row in source_file_reader:
        for source_file_column, source_file_value in source_file_row.items():
            columns_storage.setdefault(source_file_column, []).append(source_file_value)

    return columns_storage


print(get_columns_from_source_file())

我得到的是:

defaultdict(<class 'list'>, {'h1\th2\th3\th4': ['10\t11\t12\t13', '14\t15\t16\t17', '18\t19\t20\t21']})

1 个答案:

答案 0 :(得分:2)

您只需添加delimiter='\t'参数即可获得所需内容:

import csv
from collections import defaultdict

def get_columns_from_source_file():
    source_file_reader = csv.DictReader(open('test.csv'), delimiter='\t')
    columns_storage = defaultdict(list)

    for source_file_row in source_file_reader:
        for source_file_column, source_file_value in source_file_row.items():
            columns_storage.setdefault(source_file_column, []).append(source_file_value)

    return columns_storage


print(get_columns_from_source_file())

结果:

defaultdict(<class 'list'>, {'h1': ['10', '14', '18'], 'h2': ['11', '15', '19'], 'h3': ['12', '16', '20'], 'h4': ['13', '17', '21']})