i have been trying to write a program in python to read two text files and print the differences in their texts.the two files are similar other than the line numberings which are different owing to some comments that have been inserted.I have tried using difflib module but it is giving me errors.
import difflib
from difflib import *
temp3=[]
temp4=[]
with open ("seqdetect",'r') as f:
with open ("seqdetect_2",'r') as g:
for item in f:
temp1 =item.split()
temp3.append(temp1)
for items in g:
temp2 =items.split()
temp4.append(temp2)
d = difflib.Differ()
diff = d.compare(temp3, temp4)
print ('\n'.join(diff))
Could you please suggest an alternative.
Regards,
Mayank
Ok, I’ve tried out your code and found the issue.
The
Differ.compare()method expects to be given two lists of strings, representing the lines of your two texts. However, because of youritem.split()calls, your liststemp3andtemp4are lists of lists of (one character long) strings.I’m not sure exactly what you were wanting to do with that split, so I’m not sure what the best fix is. If you really do want it to tell you the individual characters that have been added or removed, you can replace your calls to
append()withextend()in the two for loops. But that doesn’t seem very useful, frankly.More likely the splitting is a mistake. Rather than looping over the lines in your files, just read them all into lists using
readlines()and let the Differ do its work on them.If you want to do some filtering on what counts as a difference (ignoring whitespace differences, or whatever) you should explore the difflib documentation, and pass an appropriate function as the
linejunkorcharjunkparameters of the Differ’s constructor.