python运维常用模块(四)——文件对比模块difflib
作者:互联网
1.difflib介绍
difflib作为Python的标准库模块,无需安装,作用是对比文本之间的差异,且支持输出可读性较强的HTML文档,与linux下的diff命令类似。我们可以使用difflib对比代码,配置文件的差别,在版本控制方面是非常有用。Python2.3或更高版本默认自带difflib模块,无需额外安装。
实例1:两个字符串差异对比
#!/usr/bin/python3
import difflib
# 定义字符串1
text1="""text1
This module provides classes and functions for comparing sequences.
including HTML and context and unified diffs.
difflib document v7.4
add string"""
# 以行进行分隔
text1_lines=text1.splitlines()
# 定义字符串2
text2="""test2
This module provides classes and functions for Comparing sequences.
including HTML and context and unified diffs.
difflib document v7.5"""
text2_lines=text2.splitlines()
# 创建Differ()对象
d=difflib.Differ()
# 采用compare方法对字符串进行比较
diff=d.compare(text1_lines,text2_lines)
print('\n'.join(list(diff)))
本示例采用Differ()类对两个字符串进行比较,另外difflib的SequenceMatcher()类支持任意类型序列的比较,HtmlDiff()类支持将比较结果输出为HTML格式
执行结果如下:
[root@prometheus01 ~]# python3 simple6.py
- text1
+ test2
- This module provides classes and functions for comparing sequences.
? ^
+ This module provides classes and functions for Comparing sequences.
? ^
including HTML and context and unified diffs.
- difflib document v7.4
? ^
+ difflib document v7.5
? ^
- add string
符号含义说明
生成美观的对比HTML格式文档
采用HtmlDiff()类,运行python3 simple7.py > diff.html
,再使用浏览器打开diff.html文件,结果如图所示,HTML文档包括了行号,差异标志,图例等信息,可读性增强了许多。make_file()方法就可以生成美观的HTML文档,代码如下:
#!/usr/bin/python3
import difflib
# 定义字符串1
text1="""text1
This module provides classes and functions for comparing sequences.
including HTML and context and unified diffs.
difflib document v7.4
add string"""
# 以行进行分隔
text1_lines=text1.splitlines()
# 定义字符串2
text2="""test2
This module provides classes and functions for Comparing sequences.
including HTML and context and unified diffs.
difflib document v7.5"""
text2_lines=text2.splitlines()
## 创建Differ()对象
#d=difflib.Differ()
## 采用compare方法对字符串进行比较
#diff=d.compare(text1_lines,text2_lines)
#print('\n'.join(list(diff)))
# 使用HtmlDiff()方法生成html风格对比文件
d=difflib.HtmlDiff()
print(d.make_file(text1_lines,text2_lines))
实例2:对比Nginx配置文件差异
当我们维护多个Nginx配置时,时常会对比不同版本配置文化的差异,使运维人员更加清晰地了解到不同版本迭代后的更新项,实现的思路是读取两个需对比的配置文件,再以换行符作为分隔符,调用difflib.HtmlDiff()生成HTML格式的差异文档,实现代码如下:
#!/usr/bin/python
#_*_coding:utf-8_*_
import difflib
import sys
try:
textfile1=sys.argv[1] #第一个配置文件路径参数
textfile2=sys.argv[2] #第二个配置文件路径参数
except Exception as e:
print("Error: " +str(e))
print("Usage: simple83.py filename1 filename2")
sys.exit()
def readfile(filename): #文件读取分隔函数
try:
fileHandle=open(filename,'rb')
text=fileHandle.read().splitlines()
fileHandle.close()
return text
except IOError as error:
print('Read file Error:' +str(error))
sys.exit()
if textfile1=="" or textfile2=="":
print("Usage: simple83.py filename1 filename2")
sys.exit()
text1_lines=readfile(textfile1)
text2_lines=readfile(textfile2)
d=difflib.HtmlDiff() #创建HtmlDiff()对象
print(d.make_file(text1_lines,text2_lines))#通过make_file的方法生成HTML文件的对比结果
nginx.conf.v1与nginx.conf.v2配置文件对比结果:
python simple8.py nginx.conf.v1 nginx.conf.v2 >diff.html
标签:HTML,运维,python,lines,difflib,text2,text1,模块,print 来源: https://www.cnblogs.com/even160941/p/16078089.html