jsonpath python,jsonpath库

  jsonpath python,jsonpath库

  #编码:utf-8#数据对比,根据数据结构递归对比类JSON compare:def _ _ init _ _(self,expect_data,real_data,is _ debug=False):self。expect _ data=expect _ data自身。real _ data=real _ data自我。data _ compare _ result=[]#数据对比结果self.frame_cmpare_result=[] #结构对比结果自我。默认root= self。比较(expect_data,real_data,self。默认root)if is _ debug:for I in self。data _ compare _ result:print(I)for I in self。frame _ cmpare _ result:print(I)def compare(self,expect _ data,real_data,path=/):try:if not is instance(expect _ data,(list,tuple,dict)): # if isinstance(real_data,unicode):real _ data=real _ data。编码( utf编码预期值:%s%s,实际值:%s%s % (path,str(expect_data),type(expect_data),str(real_data),type(real _ data))self。数据比较结果。append(msg)elif is instance(expect _ data,(list,tuple)): # list,tuple if not isinstance(real_data,(list,tuple)): raise IndexError(实际数据不是列表:%s % path) #实际数据为非列表/元组类型对于index,value in enumerate(expect _ data):try:if index len(real _ data):self。compare(value,real_data[index], % % s[% d] %(path,index)) else: raise IndexError(不存在的下标:%s[%d] % (path,index))异常为e:if索引错误:self。frame _ cmpare _ result。追加(结构异常或者数据缺失:% s % e . args)else:self。frame _ cmpare _ result。追加(未知异常:% s % e . args)else:# dict if not is instance(real _ data,dict): raise IndexError(实际数据不是字典:%s %路径)#实际数据为非词典类型对于关键字,expect _ data中的值。items():try:if key in real _ data。keys():self。compare(value,real_data[key], %s[\%s\] % (path,str(key))) else: raise IndexError(不存在的键:%s[\%s\] %(路径,str(key)))异常为e:if索引错误:self。frame _ cmpare _ result。追加(结构异常或者数据缺失:% s % e . args)else:self。frame _ cmpare _ result。追加(未知异常:% s % e . args)异常为e:self。frame _ cmpare _ result。追加(未知异常:% s % e . args)if _ _ name _ _= _ _ main _ _ :a={ a :123, b: [1,{c: 123abc , d: 123},1]} b={a: 1234, b: [1,{ c : 123 ABC }]} RES=JSON compare(a,b,is_debug=True)

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: