difflib模块详解
2021-12-20 12:48
417 查看
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)))对比程序
执行结果:
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))上面的程序
[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
对比结果如下:
参考书籍:自动化运维技术与最佳实践 刘天斯
相关文章推荐
- 【小白学AI】XGBoost推导详解与牛顿法
- php 字符串操作函数详解
- 基于jboss mod_cluster的均衡负载集群系统配置详解
- spring框架详解(二)--bean的装配
- Linux用户态程序计时方式详解
- git命令详解
- SVN安装步骤详解
- python使用协程实现并发操作的方法详解
- 亚马逊经典面试题实例详解
- Git详解之基础概念
- 【.NET】继承,组合,聚合详解
- sed命令详解
- C/C++中extern关键字详解
- WinDBG 技巧:设断点命令详解(bp, bu, bm, ba 以及bl, bc, bd, be)
- 详解UIView的frame、bounds和center属性
- Leap Motion自带Sample之详解_Win/C++版本
- RedHat系统下SVN服务器的搭建过程详解
- C++字符串函数详解
- 手机端rem如何适配_rem详解及使用方法
- spring boot 图片上传与显示功能实例详解