Is there a way to display the index in the list using difflib.unified_diff?

242 Views Asked by At

I would like to compare text files and see what sorts of changes were made. Not only would I like to see the changes, but I would like to see where these words can be found in the new list. This is the sample code in the documentation:

>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):
...     sys.stdout.write(line)   
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido

I would like it to display the index in the new list for the new words. For example, next to python, eggy, and hamster, it should say 0, 1, and 2 respectively. I tried editing difflib.py but that caused the example to fail and didn't result in any changes in the output of my regular code.

Any ideas? Thanks for your help!

0

There are 0 best solutions below