从值python获取密钥

时间:2014-07-22 15:36:12

标签: python parsing dictionary key

我有一个包含很多行的txt文件

369136986 cms_trk_dcs_05:CAEN/CMS_TRACKER_SY1527_8/branchController05/easyCrate1/easyBoard03/channel002

两列中第一列有数字,第二列是属性由/分隔的行,两列都用空格分隔。

我做了一个字典词典,其中key1是
行中出现的第一个数字(369136986),此键的值为另一个字典,其中键为cmstrktrackersybranch,{{ 1}},crateboard以及每个密钥都包含值channelcms_trk_dcs_05:CAENCMS_TRACKER_SY1527_8branchController05,{分别为{1}},easyCrate1,因此如果您easyBoard03(key2)询问channel002(key1),则会返回cmstrk(值)。

如何通过提供价值获得密钥?我的意思是如果我给出值369136986我需要知道哪个key1对应(程序应该返回cms_trk_dcs_05)。

这是我试过的:

CMS_TRACKER_SY1527_8

但它返回

369136986

那我该如何获得第一把钥匙呢?

1 个答案:

答案 0 :(得分:1)

m是在HVInfoDict [l]中复活的整个字典..你需要像这样查看m里面的值。

HVInfoDict = {
    369136986: {
        'cmstrk': 'cms_trk_dcs_05:CAEN',
        'trackersy': 'CMS_TRACKER_SY1527_8',
        'branch': 'branchController05',
        'crate': 'easyCrate1',
        'board': 'easyBoard03',
        'channel': 'channel002'
    }
}

input3 = raw_input("Write the property(s) which modules connected you want to know, separated by a single space \n > ")
input_list3 = input3.split(' ')
for k in input_list3:
    print "%r" % k
    txt.write("\t\n The modules with property %r are:\n" % k)
    for l,m in HVInfoDict.items():
        if k in m.values():
            print l
        else:
            print("Does not exist!")

输出:

Write the property(s) which modules connected you want to know, separated by a single space 
 > branchController05 CMS_TRACKER_SY1527_8 channel002 abcdefg
'branchController05'
369136986
'CMS_TRACKER_SY1527_8'
369136986
'channel002'
369136986
'abcdefg'
Does not exist!