python对比数据_python数据对比功能,可做RF关键字

#-*- coding: utf-8 -*-

importjsonimportosclassCompareData:defread_json_from_file(self,fileDir):'''读取.json文件内的json数据

入参:fileDir为文件的绝对路径

返回:.json文件里的json数据'''

#print("当前路径 -> %s" %os.getcwd())

with open(fileDir) as json_file:

json_data=json.load(json_file)returnjson_datadef compare_json_value(self,data,dataOld,*ignore):#统计丢失的数据

self.lostData =[]#统计列表多出的数据个数

self.surplusData =[]#统计不同的数据(不包括丢失的数据)

self.differentData =[]

self.compare_dic_value(data, dataOld, None,*ignore)

result= {"lostData":self.lostData,"surplusData":self.surplusData,"differentData":self.differentData}returnresultdef compare_dic_value(self,data,dataOld,dicLevel,*ignore):for item indataOld:if dicLevel ==None:

preDicLevel=itemelse:

preDicLevel= dicLevel + "." +item#print ("当前层级:" + preDicLevel)

if preDicLevel not inignore:

newValue= data.get(item, "getItemErrorCK")if newValue == "getItemErrorCK":

lost_data= {"dicLevel":preDicLevel,"dataOld":dataOld[item]}

self.lostData.append(lost_data)print('*WARN*' + '丢失数据:' + str(preDicLevel) + "--老接口数据为" +str(dataOld[item]))else:ifisinstance(dataOld[item], list):#assert len(dataOld[item]) == len(data[item])

print("是个list")

self.compare_list_value(data[item], dataOld[item], preDicLevel,*ignore)elifisinstance(dataOld[item], dict):print("是个dic")

self.compare_dic_value(data[item], dataOld[item], preDicLevel,*ignore)else:

self.compare_basic_value(data[item], dataOld[item], preDicLevel)def compare_list_value(self,data,dataOld,dicLevel,*ignore):#新数据比老数据中列表多出的数据

if len(data) >len(dataOld):for i inrange(len(dataOld), len(data)):

preDicLevel= dicLevel + "." +str(i)if preDicLevel not inignore:

surplus_data= {"dicLevel":preDicLevel,"dataError":data[i]}

self.surplusData.append(surplus_data)print('*WARN*' + '列表多出数据:' +str(preDicLevel))for i inrange(len(dataOld)):

preDicLevel= dicLevel + "." +str(i)#print ("当前层级:" + preDicLevel)

if i + 1 <=len(data):if preDicLevel not inignore:ifisinstance(dataOld[i], list):assert len(dataOld[i]) ==len(data[i])

self.compare_list_value(data[i], dataOld[i], preDicLevel,*ignore)elifisinstance(dataOld[i], dict):

self.compare_dic_value(data[i], dataOld[i], preDicLevel,*ignore)else:

self.compare_basic_value(data[i], dataOld[i], preDicLevel)else:#新数据比老数据中列表丢失的数据

lost_data = {"dicLevel":preDicLevel,"dataOld":dataOld[i]}

self.lostData.append(lost_data)defcompare_basic_value(self,data,dataOld,dicLevel):if data !=dataOld:print('*WARN*' + '数据不一致:' + str(dicLevel) + "--老接口数据为" +str(

dataOld)+ "--新接口数据为" +str(data))

dif_data= {"dicLevel":dicLevel,"dataError":data,"dataOld":dataOld}

self.differentData.append(dif_data)def compare_json_key(self,data,dataOld,*ignore):#统计丢失的数据

self.lostData =[]#统计列表多出的数据个数

self.surplusData =[]#统计不同的数据(不包括丢失的数据)

self.differentData =[]

self.compare_dic_key(data, dataOld, None,*ignore)

result= {"lostData":self.lostData,"surplusData":self.surplusData,"differentData":self.differentData}returnresultdef compare_dic_key(self,data,dataOld,dicLevel,*ignore):for item indataOld:if dicLevel ==None:

preDicLevel=itemelse:

preDicLevel= dicLevel + "." +item#print ("当前层级:" + preDicLevel)

if preDicLevel not inignore:

newValue= data.get(item, "getItemErrorCK")if newValue == "getItemErrorCK":

lost_data= {"dicLevel":preDicLevel,"dataOld":dataOld[item]}

self.lostData.append(lost_data)print('*WARN*' + '丢失数据:' + str(preDicLevel) + "--老接口数据为" +str(dataOld[item]))else:ifisinstance(dataOld[item], list):#assert len(dataOld[item]) == len(data[item])

print("是个list")

self.compare_list_key(data[item], dataOld[item], preDicLevel,*ignore)elifisinstance(dataOld[item], dict):print("是个dic")

self.compare_dic_key(data[item], dataOld[item], preDicLevel,*ignore)def compare_list_key(self,data,dataOld,dicLevel,*ignore):#新数据比老数据中列表多出的数据

if len(data) >len(dataOld):for i inrange(len(dataOld), len(data)):

preDicLevel= dicLevel + "." +str(i)if preDicLevel not inignore:

surplus_data= {"dicLevel":preDicLevel,"dataError":data[i]}

self.surplusData.append(surplus_data)print('*WARN*' + '列表多出数据:' +str(preDicLevel))for i inrange(len(dataOld)):

preDicLevel= dicLevel + "." +str(i)#print ("当前层级:" + preDicLevel)

if i + 1 <=len(data):if preDicLevel not inignore:ifisinstance(dataOld[i], list):assert len(dataOld[i]) ==len(data[i])

self.compare_list_key(data[i], dataOld[i], preDicLevel,*ignore)elifisinstance(dataOld[i], dict):

self.compare_dic_key(data[i], dataOld[i], preDicLevel,*ignore)else:#新数据比老数据中列表丢失的数据

lost_data = {"dicLevel":preDicLevel,"dataOld":dataOld[i]}

self.lostData.append(lost_data)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值