format (i ) ), "r", encoding = 'utf-8' ) as f :ĬmpInfo =json. join ( "/home/output/compare", "cmp.json". load (f ) with open ( '/home/test/result/10.json', 'r' ,encoding = 'utf-8' ) as f : With open ( '/home/output/test/10.json', 'r' ,encoding = 'utf-8' ) as f : Save the results to a file ( The results can also be processed directly ) json.loads Method reading json character string ) For relevant information, please refer to another blog written by the author : of json Read relevant data Read old and new json file ( It can also be used. Output : new Parameters are relative to old Changes in parameters Your method does not give you proper difference.Python Compare the two json file Law 1: utilize json_tools Methods in class libraries Your method gives us '' difference, but the both has the same keys.
We need to check keys only (does the both have "f" key):.I think it's good, but not for all cases. No need for 66 lines, which are completely unrelated to Django. Now generalize the mapper and use set(mapper(json1)) - set(mapper(json2)). fast and efficient.įor lists, you might use set(enumerate(list1)) - set(enumerate(list2)). Generate and render HTML Table by LLyaudetįor dicts, you might try to use set(eritems()) - set(eritems()) which will compute the difference for you.Browser-native date input field by kytta.Python Django CRUD Example Tutorial by tuts_station.Month / Year SelectDateWidget based on django SelectDateWidget by pierreben.save_diff ( ' %s - %s | %s ' % ( path, first, second ), VALUE ) return def save_diff ( self, diff_message, type_ ): message = ' %s : %s ' % ( type_, diff_message ) if diff_message not in self. else : if with_values and second != None : if first != second : self. check for equality (only if with_values is True) and return. check ( first, sec, path = new_path, with_values = with_values ) # not list, not dict.
save_diff ( ' %s - %s, %s ' % ( new_path, type ( first ), type ( second )), TYPE ) # recursive call self. elif isinstance ( first, list ): for ( index, item ) in enumerate ( first ): new_path = " %s " % ( path, index ) # try to get the same index from second sec = None if second != None : try : sec = second except ( Inde圎rror, KeyError ): # goes to difference self. check ( first, second, path = new_path, with_values = with_values ) # if object is list, loop over it and check. every key from first goes to the difference self. check ( first, sec, path = new_path, with_values = with_values ) else : # second is not dict.
save_diff ( new_path, PATH ) # prevent further values checking. has_key ( key ): sec = second else : # there are key in the first, that is not presented in the second self. %s " % ( path, key ) if isinstance ( second, dict ): if second. if len ( path ) = 0 : new_path = key else : new_path = " %s. save_diff ( message, TYPE ) if isinstance ( first, dict ): for key in first : # the first part of path must not have trailing dot. check ( second, first, with_values = with_values ) def check ( self, first, second, path = '', with_values = False ): if second != None : if not isinstance ( first, type ( second )): message = ' %s - %s, %s ' % ( path, type ( first ), type ( second )) self. check ( first, second, with_values = with_values ) if vice_versa : self. Class Diff ( object ): def _init_ ( self, first, second, with_values = False, vice_versa = False ): self.