Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore lines using difflib.ndiff?

Tags:

python

According to the documentation, you can provide a linejunk function to ignore certian lines. However, I can't get it to work. Here is some sample code for discussion:

from re import search
from difflib import ndiff 
t1 = 'one 1\ntwo 2\nthree 3'
t2 = 'one 1\ntwo 29\nthree 3'
diff = ndiff(t1.splitlines(), t2.splitlines(), lambda x: search('2', x))

My intention is to ignore the second line and diff will be a generator that doesn't show any differences.

Thanks for the help.

like image 295
behindalens Avatar asked Dec 10 '25 04:12

behindalens


1 Answers

I've recently met with the same problem.

Here's what I've found out:

cf. http://bugs.python.org/issue14332

The main intent of the *junk parameters is to speed up matching to find differences, not to mask differences.

c.f. http://hg.python.org/cpython/rev/0a69b1e8b7fe/

The patch provides a better explanation of the "junk" and "ignore" concepts in difflib docs

These junk-filtering functions speed up matching to find differences and do not cause any differing lines or characters to be ignored.

like image 183
caoanan Avatar answered Dec 12 '25 16:12

caoanan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!