简体   繁体   中英

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'? Thanks!

If you take a look at unified_diff code you will find description about a parameter called 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.

In your case, n basically indicates numbers of characters. If you assign a value to n , then you will get the correct output. This 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,n=6):
    print line

Will generate:

--- 

+++ 

@@ -1,6 +1,12 @@

 a
 b
 c
 d
 e
 f
+g
+i
+k
+l
+m
+n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM