Language: Python 3.
I am quite interested to know how to compare the below strings by ignoring the value of object "DateandTime" as it will never be the same. Hence, that alone to be ignored during comparison.
Str1='''{"Name":"Denu","Contact":12345678, "DateandTime":20200207202019}'''
Str2= '''{"Name":"Denu","Contact":12345678, "DateandTime":20200207220360}'''
Any help would be indeed appreciated.
You may easily create an identical function using the dicts in the first place. Don't convert it to a string as it already is a usable object.
Str1 = {"Name":"Denu","Contact":12345678, "DateandTime":20200207202019}
Str2 = {"Name":"Denu", "Contact":12345678, "DateandTime":20200207220360}
def isidentical(dct1, dct2):
""" Compares two dicts for equality """
ignore = ["DateandTime"]
keys1 = set(key for key in dct1 if not key in ignore)
keys2 = set(key for key in dct2 if not key in ignore)
if keys1 != keys2:
return False
for key in keys1:
if dct1[key] != dct2[key]:
return False
return True
x = isidentical(Str1, Str2)
print(x)
# True in this case
This will throw an error if one dictionary has other keys than the other one or if the values are not identical. Obviously, you could extend the ignore
list.