I looked at some answers to similar questions here, but I guess I am still not understanding something about the way difflib.ndiff() works?
I am looking at ndiff in particular because the documentation implies that, by default, the diff would be ignoring whitespace changes.
Here's a simple program where I would expect the lines in the Differ (i.e,. the return value from difflib.ndiff()) to be empty:
import difflib
# a simple set of lines
A_LINES = [
'Line 1',
'Line 2',
]
# should be same as A_LINES if whitespace is ignored
B_LINES = [
' Line 1',
' Line 2',
]
def test_2(a, b):
# differ = difflib.ndiff(a, b)
differ = difflib.ndiff(a, b, charjunk=difflib.IS_CHARACTER_JUNK)
for line in differ:
print(line)
def main(a_fn, b_fn):
test_2(A_LINES, B_LINES)
if __name__ == '__main__':
main()
difflib.IS_CHARACTER_JUNK() seems to just be a predicate that returns True on ' ' and '\t', False otherwise. Whether you invoke ndiff() by explicitly calling out the IS_CHARACTER_JUNK, or accept the default and not mention the charjunk argument, I get the same output:
- Line 1
+ Line 1
? ++
- Line 2
+ Line 2
? ++
That's not the output I would expect for a diff that is ignoring whitespace. It seems very unexpected to me, given the documentation for ndiff (see: https://docs.python.org/3/library/difflib.html). Is the documentation off, or strange, or wrong, or am I just not understanding something?
How would I call ndiff() such that there are no lines in the 'differ' generator for this example?
Any help better understanding how to do "ignore whitespace"-type diffs greatly appreciated.