首页 > 解决方案 > 两个字符串(句子)之间的区别

问题描述

我试图计算两个句子之间的差异,如下所示:

import difflib

text1_lines = "I understand how customers do their choice. Difference"
text2_lines = "I understand how customers do their choice."
diff = difflib.ndiff(text1_lines, text2_lines)

我想有所作为

但我不明白。我究竟做错了什么 ?谢谢你让我知道。

标签: pythonstring

解决方案


来自文档

import difflib
import sys

text1_lines = "I understand how customers do their choice. Difference"
text2_lines = "I understand how customers do their choice."
diff = difflib.context_diff(text1_lines, text2_lines)
for line in diff:
    sys.stdout.write(line)

输出:

*** 
--- 
***************
*** 41,54 ****
c  e  .-  - D- i- f- f- e- r- e- n- c- e--- 41,43 ----

推荐阅读