Sudipta Swarnakar Sudipta Swarnakar - 1 year ago 92
Python Question

Convert csv file into python dictionary

I want to convert

type CSV file into a dictionary having format like

Answer Source

For an input.csv CSV file holding:


This could be done as follows:

import csv

my_dict = {}

with open('input.csv') as f_input:
    for row in csv.reader(f_input):
        my_dict[row[0]] = {row[1] : row[2]}

print my_dict

Giving you:

{'A': {'B': '24'}}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download