Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!novia!news-out.readnews.com!transit3.readnews.com!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Constructing JSON data structures from non-string key python dictionaries Date: Wed, 21 Nov 2012 16:04:35 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 23 Message-ID: References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> NNTP-Posting-Host: dsl.comtrol.com X-Trace: reader1.panix.com 1353513875 1002 64.122.56.22 (21 Nov 2012 16:04:35 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 21 Nov 2012 16:04:35 +0000 (UTC) User-Agent: slrn/pre1.0.0-18 (Linux) X-Received-Bytes: 1806 Xref: csiph.com comp.lang.python:33741 On 2012-11-21, MRAB wrote: >> However, I don't know how to do that because dictionary keys in >> python need to be strings. If I try to do the following, Python,of >> course, complains that y,color,drilldown, etc are not defined. > > Just quote them: > > data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE > versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE > 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ] > > Incidentally, dictionary keys in Python don't have to be strings, but > merely 'hashable', which includes integers, floats and tuples amongst > others. I think he meant that in his use case, the Python dictionary keys must be strings, since that's what JSON requires. -- Grant Edwards grant.b.edwards Yow! I have a TINY BOWL in at my HEAD gmail.com