Jan-21-2024, 11:46 PM
(This post was last modified: Jan-21-2024, 11:46 PM by deanhystad.)
I don't have the problem you describe using difflib. Could you provide a little more context please. What are a, b and difference? Could you post a complete example?
My guess is that a and b are strings read from two files. All the lines end with a newline except the last. If that's the problem, you can hardly fault difflib that your input files don't end with an empty line.
You could do something like this, letting you control where newlines appear in the output file:
My guess is that a and b are strings read from two files. All the lines end with a newline except the last. If that's the problem, you can hardly fault difflib that your input files don't end with an empty line.
You could do something like this, letting you control where newlines appear in the output file:
from difflib import Differ with open("text.txt", "w") as file, open("a.txt", "r") as a, open("b.txt", "r") as b: for x in Differ().compare(a.readlines(), b.readlines()): print(x.strip(), file=file)