Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; "'',": 0.07; 'json': 0.07; 'wednesday,': 0.07; 'string': 0.09; '[0]': 0.09; 'assigning': 0.09; 'next,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrote': 0.14; '"2":': 0.16; '"python",': 0.16; "'0'": 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'supplied,': 0.16; 'those,': 0.16; 'variables,': 0.16; 'wrote:': 0.18; '(the': 0.22; '>>>': 0.22; 'char': 0.24; '15,': 0.26; 'header:X-Complaints-To:1': 0.27; '[1]': 0.29; '(which': 0.31; 'gives': 0.31; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; 'keyerror:': 0.31; 'wind': 0.31; 'file': 0.32; '(most': 0.33; 'subject:with': 0.35; "can't": 0.35; 'but': 0.35; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'first': 0.61; 'such': 0.63; 'here': 0.66; 'sample': 0.67; 'containing': 0.69; "'2',": 0.84; "'3',": 0.84; 'received:myvzw.com': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Parsing Python dictionary with multiple objects Date: Wed, 15 Oct 2014 18:25:19 -0400 (EDT) Organization: news.gmane.org References: <64f7e57a-4be3-44f4-b11c-0d270fc91809@googlegroups.com> <4b844872-b21e-4286-97a7-a76076f25809@googlegroups.com> <1000bb60-54bc-4aad-a3cb-77fd481af93c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 109.sub-70-192-196.myvzw.com X-Newsreader: PiaoHong.Usenet.Client.VIP:1.62 X-Archive: encrypt X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413411737 news.xs4all.nl 2838 [2001:888:2000:d::a6]:49901 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79183 Anurag Patibandla Wrote in message: > On Wednesday, October 15, 2014 1:35:43 PM UTC-4, Rustom Mody wrote: >> On Wednesday, October 15, 2014 10:51:11 PM UTC+5:30, Anurag Patibandla wrote: >> >> > Here is my sample dict if that helps: >> >> > >> >> > >> >> > >> >> > json = {"1": {"Status": "Submitted", "Startdate": ["01/01/2011"], "Enddate": ["02/02/2012"], "Job_ID": 1, "m_Quantile": "80", "m_Controller": "Python", "m_Method": "Distributed", "Allocation_3": ["50"], "Allocation_2": ["30"], "Allocation_1": ["20"], "Note": "", "m_Iterations": "1000", "submit": ["Submit"], "VaR": "", "Asset_2": ["YHOO"], "Asset_3": ["CAT"], "Asset_1": ["AAPL"]}, "3": {"Status": "Submitted", "Startdate": ["01/01/2011"], "Enddate": ["02/02/2012"], "Job_ID": 3, "m_Quantile": "90", "m_Controller": "Python", "m_Method": "Distributed", "Allocation_3": ["50"], "Allocation_2": ["30"], "Allocation_1": ["20"], "Note": "", "m_Iterations": "1000", "submit": ["Submit"], "VaR": "", "Asset_2": ["YHOO"], "Asset_3": ["CAT"], "Asset_1": ["AAPL"]}, "2": {"Status": "Submitted", "Startdat > e": ["01/01/2011"], "Enddate": ["02/02/2012"], "Job_ID": 2, "m_Quantile": "80", "m_Controller": "Python", "m_Method": "GARCH", "Allocation_3": ["50"], "Allocation_2": ["30"], "Allocation_1": ["20"], > "Note": "", "m_Iterations": "1000", "submit": ["Submit"], "VaR": "", "Asset_2": ["YHOO"], "Asset_3": ["CAT"], "Asset_1": ["AAPL"]}, "4": {"Status": "Submitted", "Startdate": ["01/01/2011"], "Enddate": ["02/02/2012"], "Job_ID": 4, "m_Quantile": "90", "m_Controller": "Python", "m_Method": "GARCH", "Allocation_3": ["50"], "Allocation_2": ["30"], "Allocation_1": ["20"], "Note": "", "m_Iterations": "1000", "submit": ["Submit"], "VaR": "", "Asset_2": ["YHOO"], "Asset_3": ["CAT"], "Asset_1": ["AAPL"]}} >> >> >> >> Right >> >> So your dict (which is dicts !) we have >> >> >>> json.keys() >> >> ['1', '3', '2', '4'] >> >> >> >> And so >> >> >> >> >>> json[0] >> >> Traceback (most recent call last): >> >> File "", line 1, in >> >> KeyError: 0 >> >> >> >> >>> json['0'] >> >> Traceback (most recent call last): >> >> File "", line 1, in >> >> KeyError: '0' >> >> >> >> >>> json['1'] >> >> {'Status': 'Submitted', 'Startdate': ['01/01/2011'], 'Enddate': ['02/02/2012'], 'Job_ID': 1, 'm_Quantile': '80', 'Allocation_3': ['50'], 'm_Method': 'Distributed', 'm_Controller': 'Python', 'Allocation_2': ['30'], 'Allocation_1': ['20'], 'Asset_2': ['YHOO'], 'Note': '', 'VaR': '', 'submit': ['Submit'], 'm_Iterations': '1000', 'Asset_3': ['CAT'], 'Asset_1': ['AAPL']} >> >> >> >> IOW 0 is not a key >> >> Neither is '0' (the string containing the char 0) >> >> But the string '1' is a valid key > > Yes, but I can't just do 'json['1']', at the end of the code I need to do a 'dicts['1']', or 'dicts['2']', to get the smaller dicts which still gives me a 'KeyError: 1' > Did you read the code I supplied, where you would wind up with three variables, dict1, ict2, and dict3? Just before assigning those, I had a LIST of dicts. Such a list can be accessed by threedicts [0] to get the first dictionary, threedicts [1] to get the next, etc. -- DaveA