对码当歌,猿生几何?

python判断两个json文件是否相等

# -*-coding:utf-8-*-import jsonimport jsonpatchimport sysdef cmp(src_data, dst_data):if isinstance(src_data, dict):"""若为dict格式"""for key in dst_data:if key not in src_data:
                print("src不存在这个key:" + key)return Falsefor key in src_data:if key in dst_data:
                thiskey = key"""递归"""# if key == "nrfProfileId":#     continue# else:if cmp(src_data[key], dst_data[key]) is False:return Falseelse:
                print("dst不存在这个key:" + key)return Falseelif isinstance(src_data, list):"""若为list格式"""if len(src_data) != len(dst_data):
            print("list len: '{}' != '{}'".format(len(src_data), len(dst_data)))return Falseif "fqdn" in src_data[0]:
            src_data.sort(key=lambda x: x["fqdn"])
            dst_data.sort(key=lambda x: x["fqdn"])elif "nrfProfileId" in src_data[0]:
            src_data.sort(key=lambda x: x["nrfProfileId"])
            dst_data.sort(key=lambda x: x["nrfProfileId"])for src_list, dst_list in zip(src_data, dst_data):"""递归"""if cmp(src_list, dst_list) is False:return Falseelse:if str(src_data) != str(dst_data):
            print("src_data: '{}' != dst_data: '{}'".format(src_data, dst_data))return Falsereturn Truedef comparetwojson(real_json, except_json):if len(real_json) == len(except_json):for j in range(len(except_json)):
            exp_profile = except_json[j]
            i = 0for i in range(len(real_json)):
                profile = real_json[i]

                p_patch = jsonpatch.JsonPatch.from_diff(profile, exp_profile)if list(p_patch) == []:
                    i -= 1print("GRPC message return profile list is the 
                                    same as the expected profile list")breakif i == len(real_json) - 1:return Falsereturn Trueelse:return Falseif __name__ == "__main__":
    xx = {"111": None,          "23456": {"33333": "0000", "22222": 9999}}

    yy = {"111": None,          "23456": {"22222": 9999, "33333": "0000"}}
    x1 = [{"nrfProfileId": "12345678-provision-0001", "servingPlmn": {"mcc": "310", "mnc": "003"},           "nrfAddresses": [{"scheme": "http", "fqdn": "192.168.240.9", "port": 5678}]},
          {"nrfProfileId": "12345678-provision-0002", "servingPlmn": {"mcc": "310", "mnc": "003"},           "nrfAddresses": [{"scheme": "http", "fqdn": "www.baidu.com", "port": 5002},
                            {"scheme": "http", "fqdn": "150.132.165.200", "port": 5003}]}]
    x2 = [{"nrfProfileId": "12345678-provision-0001", "servingPlmn": {"mcc": "310", "mnc": "003"},           "nrfAddresses": [{"scheme": "http", "fqdn": "192.168.240.9", "port": 5678}]},
          {"nrfProfileId": "12345678-provision-0002", "servingPlmn": {"mnc": "003", "mcc": "310"},           "nrfAddresses": [{"scheme": "http", "fqdn": "150.132.165.200", "port": 5003},
                            {"scheme": "http", "fqdn": "www.baidu.com", "port": 5002}]}]

    y1 = json.load(open(sys.argv[1]))
    y2 = json.load(open(sys.argv[2]))if cmp(y1, y2):
        print("equal")else:
        print("not equal")# if comparetwojson(x1, x2):#     print("相等")# else:#     print("不相等")

           

阅读更多