简体   繁体   中英

Convert CSV file to a dictionary of lists

My csv file looks like this:

Name,Surname,Fathers_name
Prakash,Patel,sudeep
Rohini,Dalal,raghav
Geeta,vakil,umesh

I want to create a dictionary of lists which should be like this:

dict = {Name: [Pakash,Rohini,Geeta], Surname: [Patel,Dalal,vakil], Fathers_name: [sudeep,raghav,umesh]}

This is my code:

with open(ram_details, 'r') as csv_file:
csv_content = csv.reader(csv_file,delimiter=',')
header = next(csv_content)
if header != None:
    for row in csv_content:
        dict['Name'].append(row[0])

It is throwing an error that key does not exists? Also, if there is any better way to get the desired output?!! Can someone help me with this?

Your code looks fine. It should work, still if you are getting into any trouble you can always use defaultdict .

from collections import defaultdict 
# dict = {'Name':[],'Surname':[],'FatherName':[]}
d = defaultdict(list)
with open('123.csv', 'r') as csv_file:
    csv_content = csv.reader(csv_file,delimiter=',')
    header = next(csv_content)
    if header != None:
        for row in csv_content:
            # dict['Name'].append(row[0])
            # dict['Surname'].append(row[1])
            # dict['FatherName'].append(row[2])
            d['Name'].append(row[0])
            d['Surname'].append(row[1])
            d['FatherName'].append(row[2])

Please don't name a variable similar to a build in function or type (such as dict ).

The problem is that you haven't initialized a dictionary object yet. So you try to add a key and value to an object which is not known to be dict yet. In any case you need to do the following:

result = dict()  # <-- this is missing
result[key] = value

Since you want to create a dictionary and want to append to it directly you can also use python's defaultdict .

A working example would be:

import csv
from collections import defaultdict
from pprint import pprint

with open('details.csv', 'r') as csv_file:
    csv_content = csv.reader(csv_file, delimiter=',')
    headers = list(map(str.strip, next(csv_content)))
    result = defaultdict(list)

    if headers != None:
        for row in csv_content:
            for header, element in zip(headers, row):
                result[header].append(element)

pprint(result)

Which leads to the output:

defaultdict(<class 'list'>,
            {'Fathers_name': ['sudeep', 'raghav', 'umesh'],
             'Name': ['Prakash', 'Rohini ', 'Geeta  '],
             'Surname': ['Patel  ', 'Dalal  ', 'vakil  ']})

Note 1) my csv file had some extra trailing spaces, which can be removed using strip() , as I did for the headers .

Note 2) I am using the zip function to iterate over the elements and headers at the same time (this saves me to index the row).

Possible alternative is using pandas to_dict method ( docs )

You may try to use pandas to achieve that:

import pandas as pd


f = pd.read_csv('todict.csv')
d = f.to_dict(orient='list')

Or if you like a one liner:

f = pd.read_csv('todict.csv').to_dict('orient='list')

First you read your csv file to a pandas data frame (I saved your sample to a file named todict.csv). Then you use the dataframe to dict method to convert to dictionary, specifying that you want lists as your dictinoary values, as explained in the documentation.

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