使用Python中的json数据匹配字典值

时间:2015-03-26 12:22:27

标签: python json

我有一个类似的json文件

{"label" :
  [
    {"confidence": 1.0, "Arm_upVector": "(0.108535, 0.987291, 0.116085)", "bone_direction": ["(0, 0, 0)", "(0.354117, -0.111147, 0.928573)", "(0.144538, -0.00496286, 0.989487)", "(0.446597, -0.15941, 0.88042)", "(-0.145324, -0.134126, 0.980251)", "(0.0181324, 0.250534, 0.967938)", "(0.0234257, 0.321893, 0.946486)", "(0.0270345, 0.370523, 0.92843)", "(-0.278899, -0.118777, 0.952947)", "(-0.233781, 0.223357, 0.946287)", "(-0.202379, 0.307555, 0.92976)", "(-0.179014, 0.365886, 0.913281)", "(-0.419468, -0.0960966, 0.902669)", "(-0.311356, 0.246008, 0.917898)", "(-0.270254, 0.328053, 0.905176)", "(-0.239766, 0.384412, 0.891482)", "(-0.545443, -0.112047, 0.830625)", "(-0.571996, 0.254741, 0.779697)", "(-0.541193, 0.297035, 0.78669)", "(-0.517904, 0.327198, 0.79039)"], "handtype": "Right hand", "hand": 1, "finger": 5, "FrameId": 132251}
    ]
}

我试图将json文件中的handtype与我的字典中的handtype匹配。

我的字典如下:

                 data1={

                'FrameId':frame.id, 
                'hand' : len(frame.hands),
                'handtype': handType,
                'Arm_upVector': str(basis.y_basis),
                'confidence': confidence,
                'finger': len(frame.fingers),
                'bone_direction' : list1
                # 'pinch_strength': pinch,
                # 'grab_strength' : strength,
                # 'vector_direction' : str(fingerDirection)

                }


        if confidence==1:
            with open('data.json') as f:
                s=json.load(f)

            for row in s['label']:

                if data1['handtype'] == s['handtype']:

                    print "match found"

我正在尝试做这类事情。请帮忙

1 个答案:

答案 0 :(得分:0)

您需要将行[' handtype']与data1 [' handtype']进行比较,如下所示:

  if confidence==1:
        with open('data.json') as f:
            s=json.load(f)

        for row in s['label']:

            if data1['handtype'] == row['handtype']:

                print "match found"