difflib模块实现文件内容差异对比,difflib是python的标准库,对比文件之间差异。且支持输出可读性较强的HTML文档,与linux下的diff命令相似。difflib对比代码,配置文件的差别。
这里要介绍一个函数
splitlines()
按行进行分割,返回一个以各行内容为元素的列表
join()
将列表中的元素以指定字符连接成一个新的字符串。
difflib.Differ()
创建一个Differ对象
compare()
对字符串进行比较
`
import difflib
text1 = “”“text1:
This modlue provides classes and functions for comparing swquences.
including HTML and context and unified diffs.
difflib docment
add string
“””
text1_lines = text1.splitlines()
text2 = “”“text2:
This module provides classes and functions for Comparing sequences
including HTML and context and unified diffs.
difflib document v7.5
“””
text2_lines = text2.splitlines()
d = difflib.Differ()
diff = d.compare(text1_lines, text2_lines)
print ‘\n’.join(list(diff)
文件内容差异对比
最新推荐文章于 2024-07-05 03:52:57 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)