Why does unified_diff method from the difflib library in Python leave out some characters?
Asked Answered
E

1

6

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!

Emmanuelemmeline answered 22/7, 2016 at 19:9 Comment(4)
They didn't change, so there's no reason to show them.Offset
But why do 'd', 'e', and 'f' show?Emmanuelemmeline
To give context to the diff. Normally you'd do this with text or code, where it's useful to see the lines leading up to the changed ones.Offset
if you do this instead: for line in unified_diff(s1, s2, n=0): , where n=0 for no context, then output for d, e, and f will no longer showCarcassonne
D
8

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
Deloresdeloria answered 22/7, 2016 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.