Rectangle 27 1

python AttributeError: 'str' object has no attribute 'get'?


for keys in interfaces:
    counters = interfaces[keys].get(u'interfaceCounters', {})
    try:
        print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets',  {})
    except AttributeError:
        # counters is not a dictionary, ignore and move on
        pass

@Wilken: glad we were both of help! Note that you can only mark one of the answers as accepted; pick the one you feel helped you the best! (And yes, it is fine if that is SmCaterpillar's answer, it is your choice).

I've been going back and forth on which one to choose, Both are very good answers that help a lot!

If some of your interfaceCounters keys reference a string instead of a nested dictionary, just use exception handling to ignore those:

This is the ask forgiveness principle; if most of your entries do have the .get() method this is simply faster than the look before you leap principle, where you test if the method is available.

Note
Rectangle 27 1

python AttributeError: 'str' object has no attribute 'get'?


for keys in interfaces:
      counters = interfaces[keys].get(u'interfaceCounters', {})
      if hasattr(counters, 'get'): 
          # Only print if counters supports `get`
          print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets',  {})

Cheers, but performance wise Martijn Pieters answer is better, but less explicit ;-)

How about simply checking if your dictionary value does provide the method/attribute you need?

Thank you I love this fix, very helpful for what we are doing with gathering nested output from switches.

Note
Rectangle 27 1

python AttributeError: 'str' object has no attribute 'get'?


for keys in interfaces:
    counters = interfaces[keys].get(u'interfaceCounters', {})
    try:
        print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets',  {})
    except AttributeError:
        # counters is not a dictionary, ignore and move on
        pass

@Wilken: glad we were both of help! Note that you can only mark one of the answers as accepted; pick the one you feel helped you the best! (And yes, it is fine if that is SmCaterpillar's answer, it is your choice).

I've been going back and forth on which one to choose, Both are very good answers that help a lot!

If some of your interfaceCounters keys reference a string instead of a nested dictionary, just use exception handling to ignore those:

This is the ask forgiveness principle; if most of your entries do have the .get() method this is simply faster than the look before you leap principle, where you test if the method is available.

Note
Rectangle 27 1

python AttributeError: 'str' object has no attribute 'get'?


for keys in interfaces:
      counters = interfaces[keys].get(u'interfaceCounters', {})
      if hasattr(counters, 'get'): 
          # Only print if counters supports `get`
          print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets',  {})

Cheers, but performance wise Martijn Pieters answer is better, but less explicit ;-)

How about simply checking if your dictionary value does provide the method/attribute you need?

Thank you I love this fix, very helpful for what we are doing with gathering nested output from switches.

Note