当前位置: 首页 > news >正文

中国企业查询网官网洛阳网站建设优化

中国企业查询网官网,洛阳网站建设优化,嘉兴做微网站多少钱,wordpress蜜蜂背景: 最近项目中有需要技术升级,开发新的 API 替换原来的老的API,其功能不变,只是换了一种新方式实现。为了保准新接口的功能,测试过程中要将新 API 返回和原来的 API 返回做对比。有必要写一个 content compare too…

背景:

最近项目中有需要技术升级,开发新的 API 替换原来的老的API,其功能不变,只是换了一种新方式实现。为了保准新接口的功能,测试过程中要将新 API 返回和原来的 API 返回做对比。有必要写一个 content compare tool,Python 就是一个很好的开发工具的利器。

Difflib:

Difflib 是 Python 的标准库模块,无需安装。作用时对比序列文本集之间的差异,并且支持输出可读性比较强的 HTML 文档。详情可以参考官网 Difflib ,下面会介绍几个常用的比较类。

class difflib.SequenceMatcher

比较两个给定的的字符串,找出相同的部分。

下面引用来自coderzcolumn python 教程中

SequenceMatcher(isjunk=None,a=‘’, b=‘’, autojunk=True) - It accepts two sequences and returns an instance of SequenceMatcher which can be used to find common subsequences.
isjunk - This parameter accepts a function that takes as input a single element of the sequence and returns True if it junk else False. We can provide function if we want junk elements by ourselves. The default is None.
autojunk - This parameter accepts boolean value. If set to True, it enables auto junk finding functionality of the algorithm which we described in the introduction section. We can disable it by setting this parameter to False. The default is True.

举例:传入 isjunk 函数(忽略字符串中’,‘和’.'的比较)和默认 None 的情况

import difflib
from difflib import SequenceMatcherl1 = "Hello, Welcome to CoderzColumn."
l2 = "Welcome to CoderzColumn, Have a Great Learning Day."
seq_mat_with_junk = SequenceMatcher(isjunk=lambda x: x in [",", "."], a=l1, b=l2, autojunk=True)
seq_mat_without_junk = SequenceMatcher(a=l1, b=l2, autojunk=True)
print("similarity with junk:{}".format(seq_mat_with_junk.ratio()))
print("similarity without junk:{}".format(seq_mat_without_junk.ratio()))# output
similarity with junk:0.5609756097560976
similarity without junk:0.5853658536585366

class difflib.Differ

跟SequenceMatcher相反,是找出文本多行中的不同部分。

Differ(linejunk=None, charjunk=None)
linejunk,charjunk 参数为一个 function,忽略哪些行或则字符的比较。

4种行输出特征符号

CodeMeaning
'- ’line unique to sequence 1
'+ ’line unique to sequence 2
’ ’line common to both sequences
'? ’line not present in either input sequence

举例:比较两字符串的不同

import difflib
from difflib import Differ
str1 = "I would like to order a pepperoni pizza.\n And you?"
str2 = "I would like to order a veggie burger.\n And you?"
str1_lines = str1.splitlines()
str2_lines = str2.splitlines()
d = difflib.Differ()
diff = d.compare(str1_lines, str2_lines)
for lines in diff:print(lines)

output:

- I would like to order a pepperoni pizza.
+ I would like to order a veggie burger.And you?

举例:比较文件内容的不同

import difflib
a = open("original.txt", "r").readlines()
b = open("modified.txt", "r").readlines()
difference = difflib.Differ(charjunk=lambda x: x in [",", ".", "-", "'"])
for line in difference.compare(a, b):print(line, end="")

class difflib.HtmlDiff

以 html 格式更友好地显示不同部分

创建 HtmlDiff 实例
HtmlDiff(tabsize=8,wrapcolumn=None,linejunk=None,charjunk=IS_CHARACTER_JUNK)

tabsize is an optional keyword argument to specify tab stop spacing and defaults to 8.
wrapcolumn is an optional keyword to specify column number where lines are broken and wrapped, defaults to None where lines are not wrapped.

比较方法,下面两个效果是一样的,都是返回 String,只是返回一个是 html file string, 一个是 html talbe sting

make_file(fromlines, tolines, fromdesc=‘’, todesc=‘’, context=False, numlines=5, *, charset=‘utf-8’)
make_table(fromlines, tolines, fromdesc=‘’, todesc=‘’, context=False, numlines=5)

fromdesc and todesc are optional keyword arguments to specify from/to file column header strings (both default to an empty string).
context and numlines are both optional keyword arguments. Set context to True when contextual differences are to be shown, else the default is False to show the full files. numlines defaults to 5. When context is True numlines controls the number of context lines which surround the difference highlights. When context is False numlines controls the number of lines which are shown before a difference highlight when using the “next” hyperlinks (setting to zero would cause the “next” hyperlinks to place the next difference highlight at the top of the browser without any leading context).

fromdesctodesc 是显示 html 结果时头部信息用来标记区别版本。
context 为 Ture 只显示不同部分,False 是显示全部
numlines 是设置前面 Diff 部分间隔的行数。

举例:

import difflib
from IPython import display
a = open("original.txt", "r").readlines()
b = open("modified.txt", "r").readlines()
difference = difflib.HtmlDiff(tabsize=2)
with open("compare.html", "w") as fp:html = difference.make_file(fromlines=a, tolines=b, fromdesc="Original", todesc="Modified")fp.write(html)
display.HTML(open("compare.html", "r").read())

输出:
在这里插入图片描述

http://www.lbrq.cn/news/2656711.html

相关文章:

  • 国外做免费网站的制作电商网站
  • 圣耀做单网站小程序开发流程详细
  • 如何自己制作简单脚本单页面seo搜索引擎优化
  • 乐清做网站的公司有哪些域名收录查询工具
  • 系统优化有什么用3分钟搞定网站seo优化外链建设
  • 专做美妆的视频网站怎么查询百度收录情况
  • 新手建设网站步骤视频营销
  • qq电脑版官方入口宁波网络推广优化方案
  • pycharm 做网站哪个好一站式营销平台
  • 京东商城网站建设策划书推广技术
  • 推荐微信网站建设站长之家seo查询官方网站
  • 推荐武汉手机网站设计搜索关键词排名查询
  • 网站的robots.txt文件新网站友链
  • 哪个网站做国内销海外的app注册推广平台
  • 免费网站建设推广页面优化的方法
  • 北京网站建设模板主题百度主页入口
  • 如何做网站标题wordpress seo教程
  • 建筑焊工证查询网站官方网微营销推广平台有哪些
  • 怎么做图片网站江门关键词优化公司
  • 重庆公司黄页企业名录seo代码优化有哪些方法
  • 电商有哪些公司百度seo排名推广
  • 南京市溧水建设局网站网络营销十大成功案例
  • 做境外域名网站seo好学吗入门怎么学
  • 网站的收费窗口怎么做七台河网站seo
  • 简历生成网站网站快速排名推荐
  • 西安市做网站公司无锡seo公司找哪家好
  • 台州市建站公司国家高新技术企业查询
  • 自己做微商想做个网站磁力搜索引擎2023
  • 建设网站项目的目的站长统计app软件
  • 网站建设模板怎么做制作网站的步骤是什么
  • Python数据分析常规步骤整理
  • 【数据分享】各省农业土地流转率(2010-2023)
  • C++虚函数表实现机制以及用C语言对其进行的模拟实现(加入了自己的思考和笔记)
  • B.10.01.6-DDD领域驱动设计:从理论到落地的完整指南
  • 解决python错误:playwright._impl._errors.TimeoutError: Timeout 30000ms exceeded.
  • 使用 Prometheus+cAdvisor 监控 Docker 容器指标