Python - 获取两个Json文件的交集

时间:2015-09-04 06:54:48

标签: python json

我正在寻找一个计算两个JSON文件交集的选项。我一直在寻找它,发现我可以使用套装来解决我的问题。这工作"好的"。但我必须得到更详细的交叉点视图。这就是问题的起点。

我如何计算交叉点:

def calcIntersect(ValidationFile, json_object1, json_object2):

with open(ValidationFile) as schema_file:
    schema = j.load(schema_file)
    js.Draft4Validator.check_schema(schema)

with open(json_object1) as spec_file:
    spec1 = j.load(spec_file, object_pairs_hook=OrderedDict)
    js.validate(spec1, schema)

with open(json_object2) as spec_file:
    spec2 = j.load(spec_file, object_pairs_hook=OrderedDict)
    js.validate(spec2, schema)

x = set(spec1) & set(spec2)

print(x)

示例数据1:

{
    "Car":{
        "Brand":"Audi",
        "Nationality":"Germany",
        "Modelname":"A6"
    },
    "Engine":{
        "cubic capacity":"2967",
        "Enginetype":"V6",
        "Fuel":"Diesel",
        "MaxSpeed":"250"

    },
    "Colors":{
        "Carcolor":"Black",
        "Interiorrcolor":"white"
    }
}

示例数据2:

{
    "Car":{
        "Brand":"Audi",
        "Nationality":"USA",
        "Modelname":"A6"
    },
    "Engine":{
        "cubic capacity":"2995",
        "Enginetype":"V6",
        "Fuel":"Petrol",
        "MaxSpeed":"250"

    },
    "Colors":{
        "Carcolor":"Black",
        "Interiorrcolor":"Black"
    }
}

示例-输出:

{'Car', 'Colors', 'Engine'}

这只是" Keys"但我需要dictonaries。现在它给了我这把钥匙,说它里面有一个交叉点。也许在' Car'档案a"奥迪"而且国籍不同,因为一辆汽车在美国生产,另一辆汽车在德国生产。但它仍然会回归“汽车”。而不是"奥迪"。

我希望我能稍微描述一下我的问题。这是我的第一个问题..

3 个答案:

答案 0 :(得分:2)

以下几行,受@ likeon的回答启发,将为您提供一个字典,其键将是您的规范中相交对象的键,值是包含相交对象的数组。

intersect = { key: [o, spec2[key]] for key, o in spec1.iteritems()
                                   if key in spec2 };

修改: 如果您使用 python 3 ,则必须使用items代替iteritems

intersect = { key: [o, spec2[key]] for key, o in spec1.items()
                                   if key in spec2 };

答案 1 :(得分:1)

为什么你不只是迭代spec1并将值与spec2进行比较:

x = {k: v for k, v in spec1.iteritems() if k in spec2 and spec2[k] == v}

答案 2 :(得分:0)

您需要一个递归解决方案:

json1 = {
    "Car": {
        "Brand": "Audi",
        "Nationality": "Germany",
        "Modelname": "A6"
    },
    "Engine": {
        "cubic capacity": "2967",
        "Enginetype": "V6",
        "Fuel": "Diesel",
        "MaxSpeed": "250"
    },
    "Colors": {
        "Carcolor": "Black",
        "Interiorrcolor": "white"
    }
}

json2 = {
    "Car": {
        "Brand": "Audi",
        "Nationality": "USA",
        "Modelname": "A6"
    },
    "Engine": {
        "cubic capacity": "2995",
        "Enginetype": "V6",
        "Fuel": "Petrol",
        "MaxSpeed": "250"
    },
    "Colors": {
        "Carcolor": "Black",
        "Interiorrcolor": "Black"
    }
}


def common_dict(d1, d2):
    output = {}
    for k in set(d1) & set(d2):
        o1, o2 = d1[k], d2[k]
        if isinstance(o1, dict) and isinstance(o2, dict):
            output[k] = common_dict(o1, o2)
        elif o1 == o2:
            output[k] = o1
    return output

print common_dict(json1, json2)
# {'Engine': {'MaxSpeed': '250', 'Enginetype': 'V6'}, 'Car': {'Brand': 'Audi', 'Modelname': 'A6'}, 'Colors': {'Carcolor': 'Black'}}