You can use difflib.Differ to return a single sequence of lines with a marker at the start of each line which describes the line. The markers tell you the following information about the line:
Marker |
Description |
'- ' |
line unique to file 1 |
'+ ' |
line unique to file 2 |
' ' |
line common to both files |
'? ' |
line not present in either input files |
You can use this information to decide how to display the data. For example, if the marker is
, you put the line both in the left and right widgets. If it's +
, you could put a blank line on the left and the actual line on the right showing that the line is unique to the text on the right. Likewise, -
means the line is unique to the left.
For example, you can create two text widgets t1
and t2
, one for the left and one for the right. You can compare two files by creating a list of lines for each and then passing them to the compare
method of the differ and then iterating over the results.
t1 = tk.Text(...)
t2 = tk.Text(...)
f1 = open("file1.txt", "r").readlines()
f2 = open("file2.txt", "r").readlines()
differ = difflib.Differ()
for line in differ.compare(f1, f2):
marker = line[0]
if marker == " ":
# line is same in both
t1.insert("end", line[2:])
t2.insert("end", line[2:])
elif marker == "-":
# line is only on the left
t1.insert("end", line[2:])
t2.insert("end", "\n")
elif marker == "+":
# line is only on the right
t1.insert("end", "\n")
t2.insert("end", line[2:])
The above code ignores lines with the marker ?
since those are extra lines that attempt to bring attention to the different characters on the previous line and aren't actually part of either file. You could use that information to highlight the individual characters if you wish.