Two request messages, judging the difference between the key and value, only Judge D2 and D1 different values, and all the different keys
ok_req={
"Version": "9.0.0",
"Is_test": True,
"Store": "",
"Urs": "",
"Device": {
"OS": "Android",
"IMEI": "99001062198893",
"device_id": "Cqlimweyytezntyyyzk5mzjmctjlnmy3zjkx",
"Mac": "02:00:00:00:00:00",
"Galaxy_tag": "Cqlimweyytezntyyyzk5mzjmctjlnmy3zjkx",
"Udid": "A34b1f67dd5797df93fdd8b072f1fb8110fd0db6",
"Network_status": "WiFi"
},
"Adunit": {
"category": "VIDEO",
"Location": "1",
"App": "7a16fbb6",
"Blacklist": ""
},
"Ext_param": {
"Is_start": 0,
"VId": "Vw0brmtev"
}
}
not_ok={
"Version": "9.0.0",
"Is_test": True,
"Urs": "1",
"Store": "",
"Device": {
"OS": "Android",
"IMEI": "99001062298893",
"device_id": "Cqlimweyytezntyyyzk5mzjmctjlnmy3zjkx",
"Mac": "02:00:00:00:00:00",
"Galaxy_tag": "Cqlimweyytezntyyyzk5mzjmctjlnmy3zjkx",
"Udid": "A34b1f67dd5797da93fdd8b072f1fb8110fd0db6",
"Network_status": "WiFi"
},
"Adunit": {
"category": "VIDEO",
"Location": "1",
"App": "7a16fbb6",
"Blacklist": ""
},
"Ext_param": {
"Is_start": 0,
"Vid": "Vw0brmtev"
}
}
Requirement Analysis of Method one:
1. Loop d1 key, through key to D2 value, not to be D2 in the KEY,D2 and D1 does not exist in the same key
2. Determine the type of value by key, if it is a continuation loop of type Dict
3. Convert the key in D1 and D2 to the set type, take the difference set, remove the key that is not the same as D1 and D2 key
def compare (dic_1,dic_2):
For K in Dic_1:
V1=dic_1.get (k)
V2=dic_2.get (k, ' get not value ') #通过k去d2里取值, D2 If there is no this key, return get not value
If Type (v1) ==dict:
Compare (V1,V2) #取值为dict类型递归
Else
If v1! = V2 and V2! = ' Get not to value ':
Print (' value is not the same: key is%s,v1 is%s,v2 is%s '% (K,V1,V2))
R1 = Set (Dic_1.keys ())
r2 = set (Dic_2.keys ())
Res=r1.symmetric_difference (R2)
Print (' Two different key in Request message: ', ', '. Join (RES))
Compare (OK_REQ,NOT_OK)
Requirement Analysis of Method two:
1. The known message is two-dimensional, create a method, the two-dimensional dictionary into a one-dimensional dictionary, key with a specific symbol connected together
2. Loop D1 key, take D2 inside to take value, if v1==v2, in D2 the key value to delete, different words, the key is D1 and D2 k-v different data
3. All the rest of the D2 is different from the D1 k-v, looping D2 the remaining data and outputting
def builddict (dict_0): #把报文的二维字典变成一维, the style of the two-dimensional dictionary becomes {one-dimensional dictionary key| | Two-dimensional key:value}
dict_t = {}
For key in Dict_0:
Value = Dict_0.get (key)
If type (value) = = Dict:
For k,v in Value.items ():
dict_t[key+ ' | | ' +k]=v
Else
Dict_t[key]=value
Return dict_t
def compare (OK_REQ,NOT_OK):
Dic_1 = Builddict (ok_req) #把ok_req报文变成一维的字典格式
dic_2 = Builddict (NOT_OK) #把not_ok报文变成一维的字典格式
For K in Dic_1:
V1 = Dic_1.get (k)
V2 = Dic_2.get (k)
If V1==v2:
Dic_2.pop (k) #把dict_2中key和value与dict_1中一样的删除
Else
Print (' dic_1 ' data in different k-v, is ' + K + ': ' +dic_1.get (k) ')
For k in dic_2: #dic_2中剩余的都是和dic_1中不一样的key
Print (' dic_2 ' k-v, ' +k+ ': ' +dic_2.get (k) ')
Compare (OK_REQ,NOT_OK)
Python Request message comparison (assuming up to two-dimensional dictionary)