2015-03-02 255 views
-1

發生錯誤我不太確定如何解決,有一個嵌套的字典,其中最後一項不包含被問及的get並給出錯誤,不知道如何要解決這個問題。AttributeError:'str'對象沒有屬性'get'

非常基本的代碼調用

for keys in interfaces: 
     counters = interfaces[keys].get(u'interfaceCounters', {}) 
     print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets', {}) 

這裏是輸出:

Management1 inOctets: 0 outOctets: 0 
    Ethernet2 inOctets: 561926946 outOctets: 49932066 
    Ethernet3 inOctets: 561926408 outOctets: 49932764 
    Ethernet1 inOctets: 319314970 outOctets: 264840242 
    Ethernet6 inOctets: 561897402 outOctets: 49930884 
    Ethernet7 inOctets: 561678430 outOctets: 50180896 
    Ethernet4 inOctets: 561927603 outOctets: 49931337 
    Ethernet5 inOctets: 561926383 outOctets: 49932935 
    Vlan1 inOctets: 
    Traceback (most recent call last): 
    File "ex1.py", line 36, in <module> 
     print keys, "inOctets:", counters.get(u'inOctets', '0'), "outOctets:", counters.get(u'outOctets', '0') 
    AttributeError: 'str' object has no attribute 'get' 

繼承人的接口尾端:

u'Management1': {u'autoNegotiate': u'off', 
        u'bandwidth': 1000000000, 
        u'burnedInAddress': u'52:54:00:01:02:d3', 
        u'description': u'', 
        u'duplex': u'duplexFull', 
        u'forwardingModel': u'routed', 
        u'hardware': u'ethernet', 
        u'interfaceAddress': [], 
        u'interfaceCounters': {u'inBroadcastPkts': 0, 
              u'inDiscards': 0, 
              u'inMulticastPkts': 0, 
              u'inOctets': 0, 
              u'inUcastPkts': 0, 
              u'inputErrorsDetail': {u'alignmentErrors': 0, 
                    u'fcsErrors': 0, 
                    u'giantFrames': 0, 
                    u'runtFrames': 0, 
                    u'rxPause': 0, 
                    u'symbolErrors': 0}, 
              u'linkStatusChanges': 2, 
              u'outBroadcastPkts': 0, 
              u'outDiscards': 0, 
              u'outMulticastPkts': 0, 
              u'outOctets': 0, 
              u'outUcastPkts': 0, 
              u'outputErrorsDetail': {u'collisions': 0, 
                    u'deferredTransmissions': 0, 
                    u'lateCollisions': 0, 
                    u'txPause': 0}, 
              u'totalInErrors': 0, 
              u'totalOutErrors': 0}, 
        u'interfaceStatistics': {u'inBitsRate': 0.0, 
              u'inPktsRate': 0.0, 
              u'outBitsRate': 0.0, 
              u'outPktsRate': 0.0, 
              u'updateInterval': 300.0}, 
        u'interfaceStatus': u'disabled', 
        u'lastStatusChangeTimestamp': 1245724638.6762877, 
        u'lineProtocolStatus': u'down', 
        u'mtu': 1500, 
        u'name': u'Management1', 
        u'physicalAddress': u'52:54:00:01:02:d3'}, 
u'Vlan1': {u'bandwidth': 0, 
       u'burnedInAddress': u'52:54:00:01:37:37', 
       u'description': u'', 
       u'forwardingModel': u'routed', 
       u'hardware': u'vlan', 
       u'interfaceAddress': [{u'broadcastAddress': u'255.255.255.255', 
            u'primaryIp': {u'address': u'10.220.88.31', 
                u'maskLen': 24}, 
            u'secondaryIps': {}}], 
       u'interfaceStatus': u'connected', 
       u'lastStatusChangeTimestamp': 1250568992.891673, 
       u'lineProtocolStatus': u'up', 
       u'mtu': 1500, 
       u'name': u'Vlan1', 
       u'physicalAddress': u'52:54:00:01:37:37'}} 

回答

1

如果您interfaceCounters一些引用字符串,而不是一個嵌套的字典,只需要使用異常處理忽略那些:

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 

這是請求寬恕原則;如果大多數條目確實具有.get()方法,則在跳過原理之前,它的速度比要快,您可以在此測試方法是否可用。

+0

謝謝你,相當新的python,並沒有想到使用嘗試除外。 – Wilken 2015-03-02 11:07:03

+0

@Wilken:很高興我們都幫忙!請注意,您只能將**一個答案標記爲已接受;選擇一個你覺得最棒的幫助! (是的,如果這是SmCaterpillar的答案,這是很好的,這是您的選擇)。 – 2015-03-02 11:25:49

+0

我一直在來回選擇哪一個,兩者都是非常好的答案,幫助很多! – Wilken 2015-03-02 14:32:47

1

怎麼樣,如果你的字典值?只需勾選確實提供了你需要的方法/屬性?

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', {}) 
+0

謝謝我喜歡這個修復,對於我們在收集交換機的嵌套輸出方面做的很有幫助。 – Wilken 2015-03-02 11:07:39

+0

乾杯,但性能明智Martijn彼得斯回答是更好的,但不太明確;-) – SmCaterpillar 2015-03-02 11:16:40