"Judith Flores" <[EMAIL PROTECTED]> wrote
I have been trying to create a dictionary of dictionaries
(and more dictionaries) from a csv file.
Your code below suffers from sloppiness in the number of []
which makes it hard to know exactly what the problem might be.
But in principle what you are doing is fine and should work
beyond 2 levels.
However I would suggest that it might be esier to use classes
instead of dictionaries. Dictionaries are fine for many things but
can become a bit tortuous to navigate and maintain.
NameDayweighttemp
name114537
name135536
name215936
I assume there should be some commas (or other separators)
in there? Othewise how do you expect the csv module to separate
the data?
row={}
maindict={}
reader=DictReader(f)
for row in reader:
maindict[row['Name']=row
I assume you have a second closing ] after Name?
Also you are overwriting the row for each name. So you only
store the last entry. So you need to separate the data further.
Something like
maindict[row[name]] [row[day]] = (row.weight, row,temp)
Which would look like
{name1: {day1: (w,t), day2 : (w,t),
day2: (w,t)},
name2: {day3: (w,t)....
Which looks a bit like what you want I think?
then I can access the weight of a given name like this:
wg=int(maindict[['name1']['weight'])
I assume thats a single open { before name1?
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' }
}
},
See above
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor