1、两个字符串对比

import difflib
text1=""" test1 #定义字符串
hellow
my name is machanwei!
difflib document v7.4
add str
"""
text1_lines=text1.splitlines() #以行进行分隔,以便进行对比
text2="""text2: #定义字符串2
hellow
my name is machangwei!
difflib document v7.5
"""
text2_lines=text2.splitlines()
d=difflib.Differ() #创建Differ()对象
diff=d.compare(text1_lines,text2_lines) #采用compare方法对字符串进行比较
print('\n'.join(list(diff)))

对比程序

执行结果:

- + 好像分别代表不同的文本,来区分文本用。这里-是1的,+是文本2的。?是有区别的地方,有区别的地方会标记箭头,只有-没有+,也就是不是成对出现应该是只有某一方有文本

2、对比文件生成html文档

执行生成html语句

(venv) D:\python_mcw>python python自动化运维书\difflib模块学习.py >>..\diffres.html

挺好对比的

import difflib
text1=""" test1 #定义字符串
hellow
my name is machanwei!
difflib document v7.4
add str
"""
text1_lines=text1.splitlines() #以行进行分隔,以便进行对比
text2="""text2: #定义字符串2
hellow
my name is machangwei!
difflib document v7.5
"""
text2_lines=text2.splitlines() # #将下面的
# d=difflib.Differ() #创建Differ()对象
# diff=d.compare(text1_lines,text2_lines) #采用compare方法对字符串进行比较
# print('\n'.join(list(diff))) #替换成下面这些:
d=difflib.HtmlDiff()
print(d.make_file(text1_lines,text2_lines))

上面的程序

3、对比文件差异

[root@hecs-358404 ~]# cat mcw.py
# __*__ coding:utf-8 _*_
#!/usr/bin/env python
import difflib
import sys try:
mcwfile1=sys.argv[1] #第一个配置文件路径参数
mcwfile2=sys.argv[2] #第二个配置文件路径参数
except Exception as e:
print("Error:"+str(e))
print("Usage: mcw.py mcwfile1 mcwfile2")
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 mcwfile1=="" or mcwfile2=="":
print("Usage: mcw.py mcwfile1 mcwfile2")
sys.exit()
text1_lines=readfile(mcwfile1) #调用函数,获取分隔后的字符串
text2_lines=readfile(mcwfile2)
d=difflib.HtmlDiff()
print d.make_file(text1_lines,text2_lines)

对比程序

server {

       listen       80;

       server_name  blog.etiantian.org;

       location / {

                root   html/blog;

                index  index.html index.htm;

          }

        location ~* .*\.(php|php5)?$ {

          root html/blog;

              fastcgi_pass  127.0.0.1:9000;

              fastcgi_index index.php;

              include fastcgi.conf;

          }

}

文件1

#server {

       listen       80;

            server_name  blog.etiantian.org;

       location / {

                root   html/blog;

                index  index.html index.htm;

          }

        machangwei
location ~* .*\.(php|php5)?$ { root html/blog; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
fffffff
}

文件2

对比结果如下:

参考书籍:自动化运维技术与最佳实践  刘天斯

difflib模块详解的更多相关文章

  1. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  2. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  3. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  4. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  5. python time 模块详解

    Python中time模块详解 发表于2011年5月5日 12:58 a.m.    位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...

  6. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  7. 小白的Python之路 day5 time,datatime模块详解

    一.模块的分类 可以分成三大类: 1.标准库 2.开源模块 3.自定义模块 二.标准库模块详解 1.time与datetime 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时 ...

  8. 小白的Python之路 day5 random模块和string模块详解

    random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...

  9. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

随机推荐

  1. Win32窗口框架

    Win32窗口框架 WindowClass 单例,负责窗口初始化注册和取消注册: 负责提供静态方法: 放在Window类内部,方便初始化时,wndProc(HandleMsgSetup)的赋值: cl ...

  2. Firewalls文件配置防火墙

    1.源文件 /usr/lib/firewalld/services 2.文件配置 cat /etc/firewalld/zones/public.xml <?xml version=" ...

  3. 基于Hyperledger Fabric实现ERC721

    介绍 超级账本(Hyperledger)项目是首个面向企业应用场景的开源分布式账本平台.由linux基金会牵头,包括 IBM 等 30家初始企业成员共同成立的. 区块链网络主要有三种类型:公共区块链. ...

  4. 浅析InnoDB引擎的索引和索引原理

    浅析InnoDB引擎的索引和索引原理 什么是InnoDB的索引 InnoDB的索引就是一颗B+树.页是InnoDB引擎在内存和磁盘之间交换数据的基本单位,页的大小一般是16KB,页的大小可以在启动My ...

  5. poj1248 (线性筛欧拉函数)(原根)

    强烈鸣谢wddwjlss 题目大意:给出一个奇素数,求出他的原根的个数,多组数据. 这里先介绍一些基本性质 阶 设\((a,m)=1\),满足\(a^r \equiv 1 \pmod m\)的最小正整 ...

  6. 2020.10.30--vj个人赛补题

    D - D CodeForces - 743A Vladik is a competitive programmer. This year he is going to win the Interna ...

  7. python T1119紧急措施

    2021-10-18 题目: 近日,一些热门网站遭受黑客入侵,这些网站的账号.密码及 email 的数据惨遭泄露.你在这些网站上注册若干账号(使用的用户名不一定相同),但是注册时使用了相同的 emai ...

  8. IDA硬编码修改SO,重新打包APK绕过FRIDA反调试

    该案例来自看雪 通过IDA修改SO 找到检测点 修改字符串 双击该字符串进入TEXT VIEW 选择 HEX VIEW 修改十六进制 右击EDIT 修改字符 右击 菜单栏-> edit -> ...

  9. selenium去特征

    code from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_ex ...

  10. 用C++生成solidity语言描述的buchi自动机的初级经验

    我的项目rvtool(https://github.com/Zeraka/rvtool)中增加了生成solidity语言格式的监控器的模块. solidity特殊之处在于,它是运行在以太坊虚拟机环境中 ...