python求两个字符串的相对补

求两个字符串的相对补,即求两个字符串的不同之处。

import difflib

str1 = "test 123 same and diff and diff2"
str2 = "test 123 same and same and same2"

d = difflib.Differ()
diff = d.compare(str1.splitlines(), str2.splitlines())
print ('\n'.join(list(diff)))

打印情况如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AG9GgG/article/details/87862369