Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79107 > unrolled thread

Parsing Python dictionary with multiple objects

Started byanuragpatibandla7@gmail.com
First post2014-10-14 08:57 -0700
Last post2014-10-15 18:25 -0400
Articles 8 on this page of 28 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Parsing Python dictionary with multiple objects anuragpatibandla7@gmail.com - 2014-10-14 08:57 -0700
    Re:Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-14 13:02 -0400
      Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 14:15 -0700
        Re: Parsing Python dictionary with multiple objects Skip Montanaro <skip.montanaro@gmail.com> - 2014-10-14 16:32 -0500
          Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 14:36 -0700
        Re: Parsing Python dictionary with multiple objects MRAB <python@mrabarnett.plus.com> - 2014-10-14 23:58 +0100
    Re:Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-14 21:57 -0400
      Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-14 19:32 -0700
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 20:40 -0700
      Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-14 21:50 -0700
      Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 13:43 -0400
        Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 11:35 -0700
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 20:41 -0700
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 08:52 -0700
      Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 09:06 -0700
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 09:28 -0700
      Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 09:43 -0700
        Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 09:59 -0700
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 09:57 -0700
      Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 13:48 -0400
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 10:00 -0700
      Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 10:10 -0700
        Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 10:20 -0700
          Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 13:50 -0400
    Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 10:20 -0700
      Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 10:35 -0700
        Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 11:27 -0700
          Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 18:25 -0400

Page 2 of 2 — ← Prev page 1 [2]


#79156

FromAnurag Patibandla <anuragpatibandla7@gmail.com>
Date2014-10-15 10:00 -0700
Message-ID<ffa7f241-f01a-4a56-b94c-b2dd07b50270@googlegroups.com>
In reply to#79107
keys = json.keys()
order = list(keys)
q1 = int(round(len(keys)*0.2))
q2 = int(round(len(keys)*0.3))
q3 = int(round(len(keys)*0.5))
b = [q1,q2,q3]
n=0
for i in b:
    queues = order[n:n+i]
        
    n = n+i
    lists = [(queues[j], json.get(queues[j])) for j in range(len(queues))] 
    
    dicts = dict(lists)
    print dicts
    print dict[0]

print dicts works as expected. It gives me the entire dictionary. But when I do dicts[0], there is the following error:
'type' object has no attribute '__getitem__'

[toc] | [prev] | [next] | [standalone]


#79157

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-15 10:10 -0700
Message-ID<7056aefb-600a-4bcf-a4f2-2b0f45824c88@googlegroups.com>
In reply to#79156
On Wednesday, October 15, 2014 10:30:49 PM UTC+5:30, Anurag Patibandla wrote:
> keys = json.keys()
> order = list(keys)
> q1 = int(round(len(keys)*0.2))
> q2 = int(round(len(keys)*0.3))
> q3 = int(round(len(keys)*0.5))
> b = [q1,q2,q3]
> n=0
> for i in b:
>     queues = order[n:n+i]

>     n = n+i
>     lists = [(queues[j], json.get(queues[j])) for j in range(len(queues))] 

>     dicts = dict(lists)
>     print dicts
>     print dict[0]


> print dicts works as expected. It gives me the entire dictionary. But when I do dicts[0], there is the following error:
> 'type' object has no attribute '__getitem__'

Do you want dict[0] ??
I think you want dicts[0]

[toc] | [prev] | [next] | [standalone]


#79159

FromAnurag Patibandla <anuragpatibandla7@gmail.com>
Date2014-10-15 10:20 -0700
Message-ID<5f3989af-31bf-4896-9770-a7703b8b9a58@googlegroups.com>
In reply to#79157
On Wednesday, October 15, 2014 1:10:41 PM UTC-4, Rustom Mody wrote:
> On Wednesday, October 15, 2014 10:30:49 PM UTC+5:30, Anurag Patibandla wrote:
> 
> > keys = json.keys()
> 
> > order = list(keys)
> 
> > q1 = int(round(len(keys)*0.2))
> 
> > q2 = int(round(len(keys)*0.3))
> 
> > q3 = int(round(len(keys)*0.5))
> 
> > b = [q1,q2,q3]
> 
> > n=0
> 
> > for i in b:
> 
> >     queues = order[n:n+i]
> 
> 
> 
> >     n = n+i
> 
> >     lists = [(queues[j], json.get(queues[j])) for j in range(len(queues))] 
> 
> 
> 
> >     dicts = dict(lists)
> 
> >     print dicts
> 
> >     print dict[0]
> 
> 
> 
> 
> 
> > print dicts works as expected. It gives me the entire dictionary. But when I do dicts[0], there is the following error:
> 
> > 'type' object has no attribute '__getitem__'
> 
> 
> 
> Do you want dict[0] ??
> 
> I think you want dicts[0]

Sorry about that. 
dicts[0] gives me a KeyError: 0

[toc] | [prev] | [next] | [standalone]


#79163

FromDave Angel <davea@davea.name>
Date2014-10-15 13:50 -0400
Message-ID<mailman.14885.1413395274.18130.python-list@python.org>
In reply to#79159
Anurag Patibandla <anuragpatibandla7@gmail.com> Wrote in message:
> On Wednesday, October 15, 2014 1:10:41 PM UTC-4, Rustom Mody wrote:
>> On Wednesday, October 15, 2014 10:30:49 PM UTC+5:30, Anurag Patibandla wrote:
>> 
>> > keys = json.keys()
>> 
>> > order = list(keys)
>> 
>> > q1 = int(round(len(keys)*0.2))
>> 
>> > q2 = int(round(len(keys)*0.3))
>> 
>> > q3 = int(round(len(keys)*0.5))
>> 
>> > b = [q1,q2,q3]
>> 
>> > n=0
>> 
>> > for i in b:
>> 
>> >     queues = order[n:n+i]
>> 
>> 
>> 
>> >     n = n+i
>> 
>> >     lists = [(queues[j], json.get(queues[j])) for j in range(len(queues))] 
>> 
>> 
>> 
>> >     dicts = dict(lists)
>> 
>> >     print dicts
>> 
>> >     print dict[0]
>> 
>> 
>> 
>> 
>> 
>> > print dicts works as expected. It gives me the entire dictionary. But when I do dicts[0], there is the following error:
>> 
>> > 'type' object has no attribute '__getitem__'
>> 
>> 
>> 
>> Do you want dict[0] ??
>> 
>> I think you want dicts[0]
> 
> Sorry about that. 
> dicts[0] gives me a KeyError: 0
> 

If the keys are all strings, why would you expect to find any
 items with an int key?

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#79160

FromAnurag Patibandla <anuragpatibandla7@gmail.com>
Date2014-10-15 10:20 -0700
Message-ID<be5af434-22ef-4c4e-b0c1-2c2c5b7b85ad@googlegroups.com>
In reply to#79107
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", "Startdate": ["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"]}}

[toc] | [prev] | [next] | [standalone]


#79161

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-15 10:35 -0700
Message-ID<4b844872-b21e-4286-97a7-a76076f25809@googlegroups.com>
In reply to#79160
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", "Startdate": ["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

[toc] | [prev] | [next] | [standalone]


#79166

FromAnurag Patibandla <anuragpatibandla7@gmail.com>
Date2014-10-15 11:27 -0700
Message-ID<1000bb60-54bc-4aad-a3cb-77fd481af93c@googlegroups.com>
In reply to#79161
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", "Startdate": ["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'

[toc] | [prev] | [next] | [standalone]


#79183

FromDave Angel <davea@davea.name>
Date2014-10-15 18:25 -0400
Message-ID<mailman.14900.1413411737.18130.python-list@python.org>
In reply to#79166
Anurag Patibandla <anuragpatibandla7@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

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web