[Tutor] Making a dictionary of dictionaries from csv file
Judith Flores
juryef at yahoo.com
Wed Dec 3 02:08:21 CET 2008
Dear Python community,
I have been trying to create a dictionary of dictionaries (and more dictionaries) from a csv file. The csv file contains longitudinal data corresponding to names. The following is just a very (very) simple example of how the data looks:
NameDayweighttemp
name114537
name135536
name215936
name233436.5
name316637
name338736.8
So far I have written this:
from csv import *
f=open('myfile.csv',"rt")
row={}
maindict={}
reader=DictReader(f)
for row in reader:
maindict[row['Name']=row
then I can access the weight of a given name like this:
wg=int(maindict[['name1']['weight'])
My question is the following:
How can I convert the csv to a dictionary that would have the following structure?
maindict = {
'name1' : {
'Day' : {
1 : { 'weight' : '45', 'temp' : '37' } ,
3 : { 'weight' : '55', 'temp' : '36' }
}
},
'name2' : { ..... # and we repeat the same structure for the rest of the names.
}
>From my code above you can of course guess that it doesn't make beyond the level of name.
Thank you very much,
Judith
More information about the Tutor
mailing list