[英]Why does unified_diff method from the difflib library in Python leave out some characters?
I am trying to check for differences between lines.我正在尝试检查行之间的差异。 This is my code:
这是我的代码:
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
It prints:它打印:
---
+++
@@ -4,3 +4,9 @@
d
e
f
+g
+i
+k
+l
+m
+n
What happened to 'a', 'b', and 'c'? “a”、“b”和“c”发生了什么? Thanks!
谢谢!
If you take a look at unified_diff
code you will find description about a parameter called n
:如果您查看
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”设置,默认为三。
In your case, n
basically indicates numbers of characters.在您的情况下,
n
基本上表示字符数。 If you assign a value to n
, then you will get the correct output. This code:如果您为
n
赋值,那么您将得到正确的 output。此代码:
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
Will generate:会产生:
---
+++
@@ -1,6 +1,12 @@
a
b
c
d
e
f
+g
+i
+k
+l
+m
+n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.