pythondifflib模块讲解示例
difflib模块提供的类和方法用来进行序列的差异化比较,它能够比对文件并生成差异结果文本或者html格式的差异化比较页面,如果需要比较目录的不同,可以使用filecmp模块。
class difflib.SequenceMatcher
此类提供了比较任意可哈希类型序列对方法。此方法将寻找没有包含‘垃圾’元素的最大连续匹配序列。
通过对算法的复杂度比较,它由于原始的完形匹配算法,在最坏情况下有n的平方次运算,在最好情况下,具有线性的效率。
它具有自动垃圾启发式,可以将重复超过片段1%或者重复200次的字符作为垃圾来处理。可以通过将autojunk设置为false关闭该功能。
- 1
- 2
- 3
- 4
- 5
- 6
class difflib.Differ
此类比较的是文本行的差异并且产生适合人类阅读的差异结果或者增量结果,结果中各部分的表示如下:
- 1
- 2

class difflib.HtmlDiff
此类可以被用来创建HTML表格 (或者说包含表格的html文件) ,两边对应展示或者行对行的展示比对差异结果。
make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
- 1
- 2
- 3
- 4
- 5
- 6
以上两个方法都可以用来生成包含一个内容为比对结果的表格的html文件,并且部分内容会高亮显示。
difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
比较a与b(字符串列表),并且返回一个差异文本行的生成器
- 1
- 2
示例:
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'):
... sys.stdout.write(line)
*** before.py
--- after.py
***************
*** 1,4 ****
! bacon
! eggs
! ham
guido
--- 1,4 ----
! python
! eggy
! hamster
guido
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
difflib.get_close_matches(word, possibilities[, n][, cutoff])
返回最大匹配结果的列表
- 1
- 2
示例:
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
['while']
>>> get_close_matches('apple', keyword.kwlist)
[]
>>> get_close_matches('accept', keyword.kwlist)
['except']
- 1
- 2
- 4
- 5
- 6
- 7
- 8
- 9
- 10
difflib.ndiff(a, b[, linejunk][, charjunk])
比较a与b(字符串列表),返回一个Differ-style 的差异结果
- 1
- 2
示例:
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
... 'ore\ntree\nemu\n'.splitlines(1))
>>> print ''.join(diff),
- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu
difflib.restore(sequence, which)
返回一个由两个比对序列产生的结果
- 1
- 2
示例
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
... 'ore\ntree\nemu\n'.splitlines(1))
>>> diff = list(diff) # materialize the generated delta into a list
>>> print ''.join(restore(diff, 1)),
one
two
three
>>> print ''.join(restore(diff, 2)),
ore
tree
emu
difflib.unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
比较a与b(字符串列表),返回一个unified diff格式的差异结果.
- 1
- 2
示例:
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):
... sys.stdout.write(line)
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
guido
实际应用示例
比对两个文件,然后生成一个展示差异结果的HTML文件
#coding:utf-8
'''
file:difflibeg.py
date:2017/9/9 10:33
author:lockey
email:lockey@123.com
desc:diffle module learning and practising
'''
import difflib
hd = difflib.HtmlDiff()
loads = ''
with open('G:/python/note/day09/0907code/hostinfo/cpu.py','r') as load:
loads = load.readlines()
load.close()
mems = ''
with open('G:/python/note/day09/0907code/hostinfo/mem.py', 'r') as mem:
mems = mem.readlines()
mem.close()
with open('htmlout.html','a+') as fo:
fo.write(hd.make_file(loads,mems))
fo.close()
运行结果:

生成的html文件比对结果: 

---------------------------------------------------------------------------------
关注微信公众号即可在手机上查阅,并可接收更多测试分享~

pythondifflib模块讲解示例的更多相关文章
- 为Lua5.3编写C模块简单示例
为Lua5.3编写C模块简单示例 一.编译安装Lua5.3 MSVC 命令行安装脚本: @echo off md bin md lib md include cd src cl /c /nologo ...
- 小白的Python之路 day5 shelve模块讲解
shelve模块讲解 一.概述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,有什么方法可以向dump多少次就dump多少次,并且load不会出 ...
- Ansible VMware模块使用示例
vmware_vm_facts模块使用示例 执行条件: 安装Pyvmimo: pip install pyvmomi 方法一,直接编写单个yaml文件: - hosts: localhost # 注 ...
- theano 模块 MLP示例
theano 模块 MLP示例,有需要的朋友可以参考下. theano教程Example: MLP: 约定数组为列向量, 层级:将多层传感器定义为一连串的层级,每个层级定义为一个类.类属性包括:权重. ...
- python中hashlib模块用法示例
python中hashlib模块用法示例 我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib ha ...
- python强大的绘图模块matplotlib示例讲解
Matplotlib 是 Python 的绘图库.作为程序员,经常需要进行绘图,在我自己的工作中,如果需要绘图,一般都是将数据导入到excel中,然后通过excel生成图表,这样操作起来还是比较繁琐的 ...
- UIPullRefreshFlash模块demo示例
UIPullRefreshFlash 模块概述:UIPullRefreshFlash模块对引擎新推出的下拉刷新接口进行了一层封装,app可以通过此模块来实现带炫酷动画效果的下拉刷新功能.使用此模块,在 ...
- python 存储引擎 mysql(库,表, 行) 单表多表操作 (foreign key) sql_mode pymysql模块讲解
##################总结############### mysql 常用数据类型 整型:tinyint int(42亿条左右) bigint 小数:float double dec ...
- Python tesserocr模块使用示例
操作系统:Win10 1709 X64 python版本:3.6.5 依赖模块:PIL.tesserocr. 需要说明的是,在windows系统上PowerShell通过PIP3 install t ...
随机推荐
- 如何获得C4C里某个code字段对应的描述信息
通过我这篇文章介绍的方法使用C4C OData服务去取服务订单数据(Sales Order): 如何用代码的方式取出SAP C4C销售订单创建后所有业务伙伴的数据 https://www.jiansh ...
- C#自定义规则对比两个集合的对象是否相等
IList<获取的类> ret = 类的结果集; return ret.Except(另一个相同类型的对象列表集, new AClassComPare()): public class A ...
- 高精度水题(POJ2109)
题目链接:http://poj.org/problem?id=2109 double 可以虽然可以表示10^-307~~~10^208,但是精确度只有16位,这个题有bug. #include < ...
- vuejs作用域插槽
作用域插槽 <div id='root'> <child> <template slot-scope='props'> <h1>{{props.item ...
- centos开启rewrite功能
首先找到 /etc/httpd/conf/httpd.conf 文件,然后修改以下两个地方: 1.取消下面一句的注释 LoadModule rewrite_module modules/mod_rew ...
- 第43章 RTC—实时时钟—零死角玩转STM32-F429系列
第43章 RTC—实时时钟 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fireg ...
- data-ng-init 指令
1.data-ng-init指令为AngularJS应用程序定义了一个初始值. 2.通常情况下,data-ng-init指令并不常用,将会使用控制器或模块来代替它.
- Map the Debris -freecodecamp算法题目
Map the Debris 1.要求 返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: ...
- Eclipse编写JavaFX环境配置
配置eclipse用于写JavaFX:1.确定JRE中有jfxrt.jar---jdk82.选中项目-->属性-->Java Build Path3.Libraries-->jre包 ...
- LArea 微信端 地址选择
最近做到一个项目,微信端的商城需要地址选择功能 在百度上看了,采用LArea.js....下载实例,在移动端模拟器上运行是比较好的, 在微信上模拟后出现很多问题, 1,出现undefined 都定义正 ...