def check_key_list(dict1):
"""
找出字典中值不为空的key
:param dict1:
:return:
"""
key_list=[]
for k,v in dict1.items():
if v==None or v=='':
pass
else:
key_list.append(k)
return key_list
def compare_two_dict(dict1, dict2):
"""
:param dict1: 期望的字典
:param dict2: json解析的字典
:return:
"""
# 从dict1中找出值不为None或者''的key存到key_list,然后比较两个字典对应key的值是否相等
key_list=check_key_list(dict1)
flag = True
keys1 = dict1.keys()
keys2 = dict2.keys()
neq_key=[]
if len(key_list) != 0:
for key in key_list:
if key in keys1 and key in keys2:
if dict1[key] == dict2[key]:
flag = flag & True
else:
flag = flag & False
neq_key.append(key)
print("{}这个key对应的值两个dict不相等".format(key))
print("期望的是:{}".format(dict1[key]))
print("***"*20)
print("解析的是:{}".format(dict2[key]))
else:
raise Exception('key_list contains error key')
else:
raise Exception('key_list is null')
if flag:
result = True
else:
result = False
return result