Re: [Tutor] Making a dictionary of dictionaries from csv file
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
Re: [Tutor] Making a dictionary of dictionaries from csv file
On Wed, Dec 3, 2008 at 3:34 AM, Alan Gauld [EMAIL PROTECTED] wrote: 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) This will not quite work. It will give KeyErrors because the second-level dicts don't exist in maindict. One way to fix this is to use collections.defaultdict: from collections import defaultdict maindict = defaultdict(dict) Now maindict uses an empty dictionary as its default value. For another level of nesting (the 'Day' level you show in your example) this becomes a bit more complex, see this discussion and the link: http://thread.gmane.org/gmane.comp.python.tutor/46006 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making a dictionary of dictionaries from csv file
How about this: def recursiveDictFactory(): return defaultdict(recursiveDictFactory) dictOfDictsOfDictsEtc = defaultdict(recursiveDictFactory) --- Richard Roadie Rich Lovely Part of the JNP|UK Famille www.theJNP.com (Sent from my iPod - please allow me a few typos: it's a very small keyboard) On 3 Dec 2008, at 11:43, Kent Johnson [EMAIL PROTECTED] wrote: On Wed, Dec 3, 2008 at 3:34 AM, Alan Gauld [EMAIL PROTECTED] wrote: 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) This will not quite work. It will give KeyErrors because the second-level dicts don't exist in maindict. One way to fix this is to use collections.defaultdict: from collections import defaultdict maindict = defaultdict(dict) Now maindict uses an empty dictionary as its default value. For another level of nesting (the 'Day' level you show in your example) this becomes a bit more complex, see this discussion and the link: http://thread.gmane.org/gmane.comp.python.tutor/46006 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Making a dictionary of dictionaries from csv file
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor