difflib模块详解
2024-09-04 22:53:13
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
对比结果如下:
参考书籍:自动化运维技术与最佳实践 刘天斯
最新文章
- Canny算子边缘检测(cvCanny)
- Java 配色方案--Dark Flash Builder - by Wilson Silva
- Modbus调试利器 Modbus Poll
- CAEmitterLayer
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- PHP学习笔记5-类的继承/方法重写
- CAPSPageMenu分页交互
- ionic3 app 退出应用程序
- hi3531的pcie atu资源重映射
- 求逆序对[树状数组] jdoj
- Flask 学习 十六 部署
- BZOJ.3551.[ONTAK2010]Peaks加强版(Kruskal重构树 主席树)
- Angular基础(六) DI
- 对haproxy文件进行增删改查
- Java基础——javaMail:使用心得
- python 正则表达式笔记
- ubuntu下修改MYSQL数据库密码
- 如何解决python连接数据库编码问题(python传数据到mysql乱码)'ascii' codec can't encode _mysql_exceptions.OperationalError: (1366, ";Incorrect string value:?
- 【Len's DMG】macOS Mojave 10.14.1 正式版 18B75 With Clover 4726原版镜像
- C++引用的用处
热门文章
- md5验证文件上传,确保信息传输完整一致
- mysql select语句查询流程是怎么样的
- FastAPI 学习之路(十九)处理错误
- 听说,99% 的 Go 程序员都被 defer 坑过
- 【转载】[经验] 嵌入式stm32实用的排序算法 - 交换排序
- javascript-jquery-文档处理
- 移动端 h5 uniapp 读,写,删本地文件或sd文件
- Docker制作能够ssh连接的镜像
- [调试笔记] 晚测5 T1 容易题
- Python:Ubuntu上出现错误 Could not load dynamic library 'libnvinfer.so.6' / 'libnvinfer_plugin.so.6'