How to create a dictionary with one key and multiple values from a CSV file?

帅比萌擦擦* 提交于 2021-02-20 04:23:45

问题


I'm trying to create a dictionary from a CSV file. The first column of the CSV file contains unique code/ keys and since the second column, there are values. Each row of the CSV file represents a unique key.

I tried to use the csv.DictReader and csv.DictWriter classes, but I could only figure out how to generate a new dictionary for each row.

This is a part of my code:

import csv

with open('input_experiment.csv', mode='r') as infile:
    reader = csv.reader(infile)
    with open('input_experiment.csv', mode='w') as outfile:
    writer = csv.writer(outfile)
    for rows in reader:
        k = rows[0]
        v = rows[1]
        mydict = {k:v for k, v in rows}
    print(mydict)

This is how my data looks:

The first column is the key and I want to create a dictionary with each row

EOLB-98      2  4   3   1   4   4   CCPZ-81 CTCB-18 VBOX-39

LKHN-41      3  3   1   1   4   3   BYAP-21 QENC-92 JSZQ-42

NWVF-51      5  3   2   4   3   5   YWVL-18 KPCC-99 FIMD-24

XVGP-15      1  4   1   1   4   1   DZCP-35 WMBB-45 XTCH-99

回答1:


csv.DictReader by default takes the first row as the keys of the dictionary, so that won't work here since you want the first column as the keys.

So you can read the csv file using csv.reader and then iterate over the rows and create your dictionary using dictionary comprehension

import csv

mydict = {}

#Open the file in read mode
with open('input_experiment.csv', mode='r') as infile:
    #Open a reader to the csv, the delimiter is a single space
    reader = csv.reader(infile, delimiter=' ', skipinitialspace=True)

    #Read into the dictionary using dictionary comprehension, key is the first column and row are rest of the columns
    mydict = { key: row for key, *row in reader }

print(mydict)

So if the input file is

EOLB-98 2 4 3 1 4 4 CCPZ-81 CTCB-18 VBOX-39
LKHN-41 3 3 1 1 4 3 BYAP-21 QENC-92 JSZQ-42
NWVF-51 5 3 2 4 3 5 YWVL-18 KPCC-99 FIMD-24
XVGP-15 1 4 1 1 4 1 DZCP-35 WMBB-45 XTCH-99

The output will be

{'EOLB-98': ['2', '4', '3', '1', '4', '4', 'CCPZ-81', 'CTCB-18', 'VBOX-39'], 
'LKHN-41': ['3', '3', '1', '1', '4', '3', 'BYAP-21', 'QENC-92', 'JSZQ-42'], 
'NWVF-51': ['5', '3', '2', '4', '3', '5', 'YWVL-18', 'KPCC-99', 'FIMD-24'], 
'XVGP-15': ['1', '4', '1', '1', '4', '1', 'DZCP-35', 'WMBB-45', 'XTCH-99']}



回答2:


If you want it such that you can access particular values for each key, you might consider having lists as values for each key.

import csv

with open('input_experiment.csv', mode='r') as infile:
    reader = csv.reader(infile)

    # Not sure why you have the next two lines
    with open('input_experiment.csv', mode='w') as outfile:
        writer = csv.writer(outfile)

    mydict = {}
    for row in reader:
        mydict[row[0]] = row[1:]
    print(mydict)


来源:https://stackoverflow.com/questions/56160038/how-to-create-a-dictionary-with-one-key-and-multiple-values-from-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!