Sometimes making one small mistake can be interpreted as a much larger mistake. This is due to the diff algorithm used, which does not guarantee to find the smallest possible diff.
It may be necessary to implement a specialised diff algorithm rather than use difflib.