文件内容差异对比
通过difflib模块实现文件内容差异对比,difflib 作为 python 的标准库模块,无需安装,作用是对比文件之间的差异,且支持输出可读性较强的 HTML 文档,与 Linux 下的 diff 命令相似。我们可以使用 difflib 对比代码,配置文件的差别,在版本控制方面有奇效
这次的演示比较简单,我们来进行两个字符串的比较import difflib
textStr1 = """this module provide class and function for compute,
'include html and test file .version 7.8"""
textStr2 = """this module provide class and function for compute,
'include html and test file .version 7.9"""
text1_line = textStr1.splitlines()
print(text1_line)
这里我们定义了两个字符串,只有版本号不同,我们采用字符串提供的splitlines()函数对第一个字符串进行处理,先打印出来看看是什么:
运行结果:['this module provide class and function for compute,', " 'include html and test file .version 7.8"]
可以看出,这个函数把我们的字符串按行拆开存储在一个 list 里
我们对两个字符串都进行相同的处理:text2_line = t