首页 > 代码库 > Python 之 Difflib
Python 之 Difflib
Python 之 Difflib
2017年7月8日
word文档地址:https://wenku.baidu.com/view/36692440854769eae009581b6bd97f192379bf57
参考书籍:《Python自动化运维 ——技术与最佳实践》 作者:李天斯
1.什么是difflib
Difflib作为python的标准库,无需安装,作用是对比文本之间的差异,而且支持输出可读性比较强的HTML文档,与Linux下的vimdiff命令类似,我们可以比对文本、配置文件之间的差异,在版本控制方面非常有用。
2.difflib的简单使用
2.1 Differ的简单使用
2.1.1 编写python代码
1 root@kali:/mnt/disk/python/difflib# cat difflib_0.py 2 3 #!/usr/bin/env python 4 5 import difflib 6 7 8 text1 = ‘‘‘ 9 10 I love HaiYan 11 12 I very love HaiYan 13 14 She‘s the one I love the most. 15 16 ‘‘‘ 17 18 text2 = ‘‘‘ 19 20 I love LiWang 21 22 I very love LiWang 23 24 I‘m his favorite person. 25 26 ‘‘‘ 27 28 d = difflib.Differ() 29 30 print (list(d.compare(text1,text2)))
2.1.2 执行脚本输出
# python difflib_0.py
[‘ \n‘, ‘ I‘, ‘ ‘, ‘ l‘, ‘ o‘, ‘ v‘, ‘ e‘, ‘ ‘, ‘+ L‘, ‘- H‘, ‘- a‘, ‘ i‘, ‘- Y‘, ‘+ W‘, ‘ a‘, ‘ n‘, ‘+ g‘, ‘ \n‘, ‘ I‘, ‘ ‘, ‘ v‘, ‘ e‘, ‘ r‘, ‘ y‘, ‘ ‘, ‘ l‘, ‘ o‘, ‘ v‘, ‘ e‘, ‘ ‘, ‘+ L‘, ‘- H‘, ‘- a‘, ‘ i‘, ‘- Y‘, ‘+ W‘, ‘ a‘, ‘ n‘, ‘+ g‘, ‘ \n‘, ‘- S‘, ‘+ I‘, "+ ‘", ‘+ m‘, ‘+ ‘, ‘ h‘, ‘+ i‘, ‘- e‘, "- ‘", ‘ s‘, ‘ ‘, ‘+ f‘, ‘+ a‘, ‘+ v‘, ‘+ o‘, ‘+ r‘, ‘+ i‘, ‘ t‘, ‘- h‘, ‘ e‘, ‘ ‘, ‘+ p‘, ‘+ e‘, ‘+ r‘, ‘+ s‘, ‘ o‘, ‘ n‘, ‘- e‘, ‘- ‘, ‘- I‘, ‘- ‘, ‘- l‘, ‘- o‘, ‘- v‘, ‘- e‘, ‘- ‘, ‘- t‘, ‘- h‘, ‘- e‘, ‘- ‘, ‘- m‘, ‘- o‘, ‘- s‘, ‘- t‘, ‘ .‘, ‘ \n‘]
输出了看不懂的列表,打印列表后再进行查看
增加代码:
1 list1 = list(d.compare(text1,text2)) 2 3 for line in list1: 4 5 if line == "\n": 6 7 print ("\n") 8 9 print ("%s" %(line),end=‘‘)
执行代码:
# python3 difflib_0.py I l o v e + L- H- a i- Y+ W a n+ g I v e r y l o v e + L- H- a i- Y+ W a n+ g - S+ I+ ‘+ m+ h+ i- e- ‘ s + f+ a+ v+ o+ r+ i t- h e + p+ e+ r+ s o n- e- - I- - l- o- v- e- - t- h- e- - m- o- s- t .
符号含义:
+:包含在第一个序列中,但不包含第二个序列
-:包含在第二个序列中,但是不包含第一个序列
2.2 HtmlDiff的简单使用
2.2.1 向文件写入内容
1 # echo -e "I love HaiYan \nI very love HaiYan \nShe‘s the one I love the most." > test_1 2 3 # echo -e "I love LiWang \nI very love LiWang \nI‘m his favorite person" > test_2
2.2.2 编写python代码
1 # cat difflib_1.py 2 3 #!/usr/bin/env python 4 5 import difflib 6 7 def open_files(filename): 8 9 files = open(filename,‘rb‘) 10 11 text = files.read().splitlines() 12 13 files.close() 14 15 return text 16 17 d = difflib.HtmlDiff() 18 19 20 text_1 = open_files(‘test_1‘) 21 22 text_2 = open_files(‘test_2‘) 23 24 print (d.make_file(text_1,text_2))
2.2.3 执行脚本,用网页打开
1 # python difflib_1.py > /mnt/disk/html/index.html
3.difflib案例
3.1 需求
需求:利用python实现一个功能,只需要执行[python脚本名称 文件1 文件2],只需要打开浏览器输入网址就能够看见文件比对效果
3.2 流程图
流程图:
3.3 代码编写:
1 #cat difflib_2.py 2 3 #!/usr/bin/env python 4 5 #exit argv 6 7 import sys 8 9 #path 10 11 import os 12 13 #HtmlDiff 14 15 import difflib 16 17 html_files = ‘/mnt/disk/html/index.html‘ 18 19 #Determine whether the parameter exists 20 21 try: 22 23 script_name = sys.argv[0] 24 25 file1 = sys.argv[1] 26 27 file2 = sys.argv[2] 28 29 except: 30 31 print ("%s Using: %s filename1 filename 2" %(script_name,script_name)) 32 33 sys.exit() 34 35 #Function 1 36 37 def dealwith_files(filename): 38 39 #open files 40 41 try: 42 43 files = open(filename,‘rb‘) 44 45 #read files 46 47 text = files.read().splitlines() 48 49 #close files 50 51 files.close() 52 53 except: 54 55 print ("Open files fail ") 56 57 sys.exit() 58 59 #return files 60 61 return text 62 63 #Determine if the files exists 64 65 if os.path.isfile(file1) and os.path.isfile(file2): 66 67 d = difflib.HtmlDiff() 68 69 try: 70 71 print_files = open(html_files,‘w‘) 72 73 print_files.write(d.make_file(dealwith_files(file1),dealwith_files(file2))) 74 75 print_files.close() 76 77 except: 78 79 print ("write %s fail" %(html_files)) 80 81 # print (d.make_file(dealwith_files(file1),dealwith_files(file2))) 82 83 else: 84 85 print ("%s or %s is not such file" %(file1,file2)) 86 87 sys.exit()
3.4 执行脚本输出
1 # chmod 755 difflib_2.py 2 3 # ./difflib_2.py debconf.conf debconf.conf.bak 4 5 #
chmod 是赋予脚本执行权限,执行difflib_2py 参数为debconf.conf debconf.conf.bak,没有任何输出,则证明执行OK
3.5 效果
刷新网页
Python 之 Difflib
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。