Python - json_tools
pip install json_tools
import json_tools
def json_diff(json_1, json_2):
result = json_tools.diff(json_1, json_2)
print(result)
a = {"rd": "yanan", "pro": {"sh": "shandong", "city": ["zibo", "weifang"]}}
b = {"rd": "Yanan", "pro": {"sh": "shandong", "town": ["taian", "weifang"]}}
json_diff(a, b)
import pytest
import requests
from deepdiff import DeepDiff
class TestCase(object):
expect = {
"title": "V2EX",
"slogan": "way to explore",
"description": "创意工作者们的社区",
"domain": "www.v2ex.com"
}
def setup_class(self):
self.response = requests.get('https://www.v2ex.com/api/site/info.json').json()
def test_case_01(self):
assert not DeepDiff(self.response, self.expect)
def test_case_02(self):
assert not DeepDiff(self.response['title'], 'v2ex')
"""
self =
def test_case_02(self):
> assert not DeepDiff(self.response['title'], 'v2ex')
AssertionError: {'values_changed': {'root': {'new_value': 'v2ex', 'old_value': 'V2EX'}}} != {}
E AssertionError: assert not {'values_changed': {'root': {'new_value': 'v2ex', 'old_value': 'V2EX'}}}
E + where {'values_changed': {'root': {'new_value': 'v2ex', 'old_value': 'V2EX'}}} = DeepDiff('V2EX', 'v2ex')
temp.py:104: AssertionError
"""
if __name__ == '__main__':
pytest.main(['-v', __file__])
Python3标准库:difflib差异计算工具
import difflib
s1 = [1, 2, 3, 5, 6, 4]
s2 = [2, 3, 5, 4, 6, 1]
print('Initial data:')
print('s1 =', s1)
print('s2 =', s2)
print('s1 == s2:', s1 == s2)
print()
matcher = difflib.SequenceMatcher(None, s1, s2)
for tag, i1, i2, j1, j2 in reversed(matcher.get_opcodes()):
if tag == 'delete':
print('Remove {} from positions [{}:{}]'.format(
s1[i1:i2], i1, i2))
print(' before =', s1)
del s1[i1:i2]
elif tag == 'equal':
print('s1[{}:{}] and s2[{}:{}] are the same'.format(
i1, i2, j1, j2))
elif tag == 'insert':
print('Insert {} from s2[{}:{}] into s1 at {}'.format(
s2[j1:j2], j1, j2, i1))
print(' before =', s1)
s1[i1:i2] = s2[j1:j2]
elif tag == 'replace':
print(('Replace {} from s1[{}:{}] '
'with {} from s2[{}:{}]').format(
s1[i1:i2], i1, i2, s2[j1:j2], j1, j2))
print(' before =', s1)
s1[i1:i2] = s2[j1:j2]
print(' after =', s1, '\n')
print('s1 == s2:', s1 == s2)