python difflib模块_自动化运维脚本-difflib模块

difflib模块说明:

difflib模块是python中自带的模块,无需安装。作用是对比文本之间的差异,和Linux命令diff类似,但difflib支持输出可读性较高的HTML形式展示

先通过个小案例初步认识下怎么使用:

#!/usr/local/python3/bin/python3.6

import difflib

text1 = '''

Starry starry night

paint your palette blue and grey

look out on a summer's day

with eyes that know the darkness in my soul

Shadows on the hills

sketch the trees and daffodils

catch the breeze and the winter chills

in colors on the snowy linen land

'''

text2 = '''

Starry starry day by day

paint your palette blue and grey

look out on a summer's day

with eyes that know the darkness in my soul

Shadows on the hills

sketch the trees and daffodils

catch the breeze and the winter chills

in colors on the snowy linen land

'''

file1 = text1.splitlines()

file2 = text2.splitlines()

d = difflib.Differ() #创建Differ对象

result = d.compare(file1,file2) #采用compare方法对字符串进行比较,得到一个generator

print('\n'.join(list(result)))

得到的返回结果:

380fb5e3d0c0dceee4dc709706d3a9ff.png

展示页面文本的不同,我们对展示输出的符号做下说明:

符号说明

-在第一个序列行中,但不在第二个序列行中

+在第二个序列行中,但不在第一个序列行中

''两个序列行一致

?标志出2个序列行存在增量差异

^标志出2个序列行存在的差异字符

命令行输出效果不是很直观,我们通过输出成HTML格式进行展示

#!/usr/local/python3/bin/python3.6

import difflib

text1 = '''

Starry starry night

paint your palette blue and grey

look out on a summer's day

with eyes that know the darkness in my soul

Shadows on the hills

sketch the trees and daffodils

catch the breeze and the winter chills

in colors on the snowy linen land

'''

text2 = '''

Starry starry day by day

paint your palette blue and grey

look out on a summer's day

with eyes that know the darkness in my soul

Shadows on the hills

sketch the trees and daffodils

catch the breeze and the winter chills

in colors on the snowy linen land

'''

file1 = text1.splitlines()

file2 = text2.splitlines()

d = difflib.HtmlDiff()

result = d.make_file(file1,file2)

print(result)

#输出从定向到文本中

python3.6 diff2.py >aa.html

9ce26abe6ae1aa3c1ae5a45f65502787.png

difflib模块中方法说明:

import difflib #导入模块

d = difflib.Diff() #创建Diff对象

d.compare(file1,file2) #比较文本,得到generator

dd = difflib.HtmlDiff #HtmlDiff类可以实现HTML格式

dd.make_file(file1,file2) #实现对文本的比较

最后,我们看个实际案例:生产中我们会经常修改nginx的配置文件,太多次的修改导致我们无法确定文本的差异,通过该模块写个脚本可以明确的展示出文本间的差异性

import difflib

import sys

try:

args1 = sys.argv[1] #获取外部第一个参数

args2 = sys.argv[2] #获取外部的第二个参数

except Exception as e:

print("error:" + str(e))

sys.exit()

def read_file(file):

try:

with open(file) as rstream:

text = rstream.read().splitlines()

return text

except Exception as e:

print("error:file error" +str(e))

sys.exit()

if args1 =="" or args2 == "":

print("pls enter two file")

sys.exit()

test1 = read_file(args1)

test2 = read_file(args2)

d = difflib.HtmlDiff()

result = d.make_file(test1,test2)

print(result)

通过执行脚本, 输入2个参数(对比的文件),得到HTML文件,然后浏览器展示:

d944f167c05196e06be67399a573e955.png

原文链接:https://blog.csdn.net/weixin_41910699/article/details/110114217

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值