I am trying to make comprehensive diff that compares command line output of two programs. I used
from difflib import Differ
from pprint import pprint
return line.replace("\n", "\\n")
# Two strings are expected as input
def print_diff(text1, text2):
d = Differ()
text1 = text1.splitlines(True)
text2 = text2.splitlines(True)
text1 = [readable_whitespace(line) for line in text1]
text1 = [readable_whitespace(line) for line in text2]
result = list(d.compare(text1, text2))
A = "AAABAAA\n"
A += "BBB\n"
B = "AAAAAAA\n"
B += "\n"
B += "BBB"
- AAAAAAA\n? ^^
The main problem with your example is how you are handling endline characters. If you completely replace them in the input, the output will no longer line up correctly, and so won't make any sense. To fix that, the
readable_whitespace function should look something like this:
def readable_whitespace(line): end = len(line.rstrip('\r\n')) return line[:end] + repr(line[end:])[1:-1] + '\n'
This will handle all types of endline sequence, and ensures that the lines are displayed correctly when printed.
The other minor problem is due to a typo:
text1 = [readable_whitespace(line) for line in text1] text1 = [readable_whitespace(line) for line in text2] # --^ oops!
Once these fixes are made, the output will look like this:
- AAABAAA\n ? ^ + AAAAAAA\n ? ^ + \n - BBB\n ? -- + BBB
which should hopefully now make sense to you.