I'm new to python and trying to do the following:
Open a CSV file with 5 different values separated by comma.
I want to store the data in a dictionary which key will be row[0].
Suppose there are 4 rows so 4 primary keys, for each key I need to create another nested dictionary with 4 new keys, each key with the values of rows 1, 2, 3 and 4.
Let's say the CSV contains:
1,hi,1,2,3
2,yes,2,3,4
3,no,3,4,5
4,why,4,5,6
and I need to store:
dict={'1': { 'key2':hi,'key3':1,'key4':2,'key5':3}, '2':{ }, '3':{}, '4':{} }
This is the code I'm using
import csv
dict={}
reader = csv.reader(open('filename.csv', 'r'))
for row in reader:
key=row[0]
for key in dict:
dict[key]={}
dict[key][key2]=row[1]
dict[key][key3]=row[2]
dict[key][key4]=row[3]
dict[key][key5]=row[4]
What am I missing?