我正在尝试检查行之间的差异。这是我的代码:
from difflib import unified_diff
s1 = ['a', 'b', 'c', 'd', 'e', 'f']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n']
for line in unified_diff(s1, s2):
print line
它打印:
---
+++
@@ -4,3 +4,9 @@
d
e
f
+g
+i
+k
+l
+m
+n
“a”、“b”和“c”发生了什么?谢谢!
请您参考如下方法:
如果您查看unified_diff 代码,您会发现有关名为n 的参数的描述:
Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three.
在您的情况下,n 基本上表示字符数。如果您为 n 赋值,那么您将获得正确的输出。这段代码:
from difflib import unified_diff
s1 = ['a', 'b', 'c', 'd', 'e', 'f']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n']
for line in unified_diff(s1, s2,n=6):
print line
将生成:
---
+++
@@ -1,6 +1,12 @@
a
b
c
d
e
f
+g
+i
+k
+l
+m
+n
