python标准库difflib介绍

python标准库difflib介绍

简介

从python2.3起difflib成为了标准库。无需安装即可使用。
之前遇到一个问题,我本地的代码没有问题,而服务器上的则一直报404错误。肉眼对比了一下路径,感觉是完全一样的。在网上找了下字符串对比工具,没有一个好用。
后来还是有个同事眼力好,发现有个一字符的大小写问题导致的。那会如果知道python有difflib库的话,早就把问题解决了。

简单的对比

以下示例是一个相当简单的代码。

import difflib

text1 = """
<!-- 
Template Name: Metronic - Responsive Admin Dashboard Template build with Twitter Bootstrap 3.3.6
Version: 4.5.4
Author: KeenThemes
Website: http://www.keenthemes.com/
Contact: support@keenthemes.com
Follow: www.twitter.com/keenthemes
Like: www.facebook.com/keenthemes
License: You must have a valid license purchased only from themeforest(the above link) in order to legally use the theme for your project.
-->
"""

text2 = '''
<!-- 
Template Name: Metronic - Responsive Admin Dashboard Template build with Twitter Bootstrap 3.3.6
Version: 4.5.5
Author: alston 
Contact: support@keenthemes.com
Follow: www.twitter.com/keenthemes
Like: www.facebook.com/Keenthemes
Purchase: http://themefrest.net/item/metronic-responsive-admin-dashboard-template/4021469?ref=keenthemes
License: You must have a valid license purchased only from themeforest(the above link) in order to legally use the theme for your project.
-->
'''

text1_lines = text1.splitlines()
text2_lines = text2.splitlines()

d = difflib.Differ()
diff = d.compare(text1_lines, text2_lines)

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

输出示例:

<!–
Template Name: Metronic – Responsive Admin Dashboard Template build with Twitter Bootstrap 3.3.6
– Version: 4.5.4
? ^

看着真是费劲,-,+,?,。能感觉出来-是少了,+是增加了,?和表示存在差异。

试试html美化的

修改比较简单。只要把

d = difflib.Differ()
diff = d.compare(text1_lines, text2_lines)

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

替换成

d = difflib.HtmlDiff()
print(d.make_file(text1_lines, text2_lines))

即可。

看着还不错。会用颜色标识出来。

更多对比工具

标准库里还有个filecmp库,支持单文件对比,多文件对比,目录对比。

发表评论

电子邮件地址不会被公开。 必填项已用*标注