qre0ct
qre0ct

Reputation: 6162

Unable to fetch values from a Dictionary in python

I am sure it is something very fundamental that I am missing out here, however I have banged my head and not able to get to the bottom of it. Hence, need help if someone can please :

So, I have a dictionary of the form

myDic = {
         'request_path':['api', 'v1', 'settings'], 
         'request_headers':[['Accept', 'application/json; charset=utf-8'], ['app-version', '1.0.17'], ['version-code', '40'], ['os-version', '4.3'], ['deviceId', '000000000000000'], ['User-Agent', 'App-Android'], ['Host', 'api.applic.in'], ['Connection', 'Keep-Alive'], ['Accept-Encoding', 'gzip']]
        }

So the request_path key has a value that's a list. And the request_headers key has a value that's a list of lists.

Now in my code I am trying to do something like (among other things) :

componentSize = len(myDic.get('request_path'))
genericCounter =0 
while (genericCounter < componentSize):
    print str(genericCounter+1) + "." + str(myDic.get('request_path')[genericCounter])
    genericCounter = genericCounter + 1 

And this gives me the expected result as :

1.api
2.v1
3.settings

However, the below, replaced in the above,

print str(myDic.get('request_headers')[genericCounter][0]) or
print str(myDic.get('request_headers')[genericCounter][0][0]) or
print str(myDic.get('request_headers')[genericCounter][0][0][0])

gives me an error as :

  File "myRep.py", line 393, in tamperData
    print str(myDic.get('requestHeaders')[genericCounter][0][0][0])
  File "/Library/Python/2.7/site-packages/netlib/odict.py", line 42, in __getitem__
    k = self._kconv(k)
  File "/Library/Python/2.7/site-packages/netlib/odict.py", line 206, in _kconv
    return s.lower()
AttributeError: 'int' object has no attribute 'lower'

Strangely when I try

print str(myDic.get('request_headers')[genericCounter][0])

from the python prompt - it gives me the 1st list that is :

['Accept', 'application/json; charset=utf-8']

Now I tried debugging the problem and went to /Library/Python/2.7/site-packages/netlib/odict.py as indicated in the error and looks like get(key) returns a list type of the values for key passed. So it made sense as to why the 1st one of my attempts was working fine.

But in the second case as well (request_headers), get(key) should have returned a list, isn't it (just that here it would be a list of lists) ? And is that the problem at all in the first place.

Upvotes: 1

Views: 1264

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121744

You have a netlib.odict.ODict() object; it is not a list, even though their repr() return value (unfortunately) looks like one. You cannot use integers to index into this structure directly.

Treat it as a dictionary instead; their keys are case-insensitive:

print myDic['request_headers']['accept']

Don't use a while loop with a counter; Python for loops give you the objects directly. Looping over a dictionary, for example, gives you the keys in the dictionary. The ODict() type acts like that too:

for header_name in myDic['request_headers']:
    print header_name, myDic['request_headers'][header_name]

or including the values in the loop:

for header_name, header_value in myDic['request_headers'].items():
    print header_name, header_value

Using a while loop would get cumbersome:

keys = myDic['request_headers'].keys()
index = 0
while index < len(keys):
    header_name = keys[index]
    print header_name, myDic['request_headers'][header_name]

Upvotes: 2

Related Questions