简体   繁体   English

当键怪异时将列表映射到字典

[英]mapping a list to a dict when the keys are weird

I want to process LLDP data from a TP-Link switch in an inventory plugin for Check_MK. 我想处理来自Check_MK清单插件中TP-Link交换机的LLDP数据。 The TP-Link switch doesn't use the standard SNMP OIDs for LLDP and their custom MIB has a weird quirk. TP-Link交换机不对LLDP使用标准的SNMP OID,并且它们的自定义MIB有一个怪异的现象 Instead of having an index at the end of the OIDs they put it in the middle of the OID. 而不是在OID的末尾添加索引,而是将其放在OID的中间。

[[[u'1.1.99353.1', u'Te1/0/25'], [u'1.2.99353.1', u'1'], [u'1.3.99353.1', u'MAC address'], [u'1.4.99353.1', u'00:zzzzzzz'], [u'1.5.99353.1', u'MAC address'], [u'1.6.99353.1', u'00:zzzzzzzz'], [u'1.7.99353.1', u'120'], [u'1.8.99353.1', u'Port 25'], [u'1.9.99353.1', u'THE_HOST_NAME'], [u'1.11.99353.1', u'Bridge Router'], [u'1.12.99353.1', u'Bridge Router'], [u'shortened', u'for brevity']] [[[u'1.1.99353.1',u'Te1 / 0/25'],[u'1.2.99353.1',u'1'],[u'1.3.99353.1',u'MAC地址'],[ u'1.4.99353.1',u'00:zzzzzzz'],[u'1.5.99353.1',u'MAC地址'],[u'1.6.99353.1',u'00:zzzzzzzz'',[u'1.7 .99353.1',u'120'],[u'1.8.99353.1',u'Port 25'],[u'1.9.99353.1',u'THE_HOST_NAME'],[u'1.11.99353.1',u'Bridge路由器'],[u'1.12.99353.1',u'桥路由器'],[u'shortened',u'for br'ity']

So on planet normal, I would expect things like 99353. 8 and 99353. 9 or maybe 99353.1. 因此,在正常的星球上,我期望像99353. 8和99353. 9或99353.1。之类的东西。 8 and 99353.1. 8和99353.1。 9 . 9 What they do here (1. X .99353.1) is odd. 他们在这里所做的工作(1. X .99353.1)很奇怪。 I am not sure what to do with it. 我不确定该怎么办。 All I know is I have to normalize it and I'm too stupid to do that. 我所知道的是我必须将其标准化,而我太愚蠢了。

This is what I would like to make from it: 这是我想从中得到的:

{
    l_id : 99353.1  # from the "index"
    l_ifname   : u'Te1/0/25'      # from 1.1
    r_ifname   : u'Port 25'       # from 1.8
    r_hostname : u'THE_HOST_NAME' # from 1.9.
}

Mapping this (only a subset of the list, while splitting up the key-to-be is completely above my skill level. I would like to avoid spending half a day to produce something ugly with a pile of for-loops. especially since this should go upstream to a community project and I don't want anyone to hurt their eyes. 映射它(只是列表的一个子集,同时拆分要创建的键完全超出了我的技能水平。我想避免花半天时间用一堆for循环生成难看的东西。尤其是因为这应该去社区项目上游,我不希望任何人伤到他们的眼睛。

Is there some smart approach that lets me break this into 2-3 smaller problems? 有什么聪明的方法可以让我将其分解为2-3个较小的问题吗?

You can use string.split to make a dict of the indexes: 您可以使用string.split来编写索引的字典:

list_ = [[u'1.1.99353.1', u'Te1/0/25'], [u'1.2.99353.1', u'1'], [u'1.3.99353.1', u'MAC address'], [u'1.4.99353.1', u'00:zzzzzzz'], [u'1.5.99353.1', u'MAC address'], [u'1.6.99353.1', u'00:zzzzzzzz'], [u'1.7.99353.1', u'120'], [u'1.8.99353.1', u'Port 25'], [u'1.9.99353.1', u'THE_HOST_NAME'], [u'1.11.99353.1', u'Bridge Router'], [u'1.12.99353.1', u'Bridge Router'], [u'shortened', u'for brevity']]


dict_ = {key.split(".")[1]: val for key, val in list_[:-1]}

which gives you 这给你

{'1': 'Te1/0/25',
 '11': 'Bridge Router',
 '12': 'Bridge Router',
 '2': '1',
 '3': 'MAC address',
 '4': '00:zzzzzzz',
 '5': 'MAC address',
 '6': '00:zzzzzzzz',
 '7': '120',
 '8': 'Port 25',
 '9': 'THE_HOST_NAME'}

From there it's easy to make the dictionary you're after 从那里很容易制作您想要的字典

output = {
     "l_id": list_[0][0].split(".", 2)[-1],
     "l_ifname": dict_["1"],
     "l_rname": dict_["8"],
     "r_hostname": dict_["9"],
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM