Parsing Python dictionary with multiple objects

Dave Angel davea at davea.name
Wed Oct 15 18:25:19 EDT 2014


Anurag Patibandla <anuragpatibandla7 at gmail.com> 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 "<stdin>", line 1, in <module>
>> 
>> KeyError: 0
>> 
>> 
>> 
>> >>> json['0']
>> 
>> Traceback (most recent call last):
>> 
>>   File "<stdin>", line 1, in <module>
>> 
>> 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




More information about the Python-list mailing list