python包使用_Python 包用法

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值