如何将python的difflib.unified_diff的输出应用于原始字符串?

时间:2012-04-05 17:53:01

标签: python diff

因此,使用Python的difflib,我可以生成两个字符串的差异:

foo = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Nullam sed orci lobortis lectus bibendum vehicula.\n Integer iaculis eros porttitor velit porttitor scelerisque.\n Nunc venenatis nibh.'.splitlines()
bar = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Mauris placerat, odio ut viverra gravida, elit leo tincidunt nisi, a pellentesque velit arcu id magna.\n Integer iaculis eros porttitor velit porttitor scelerisque.\n Nunc venenatis nibh.'.splitlines()

diff = difflib.unified_diff(foo, bar, lineterm='')

如果我们愿意,我们可以查看差异:

>>> print '\n'.join(diff)
--- 
+++ 
@@ -1,4 +1,4 @@
 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- Nullam sed orci lobortis lectus bibendum vehicula.
+ Mauris placerat, odio ut viverra gravida, elit leo tincidunt nisi, a pellentesque velit arcu id magna.
  Integer iaculis eros porttitor velit porttitor scelerisque.
  Nunc venenatis nibh.

这就是我被困住的地方。如何将差异应用于字符串foo以生成字符串bar

1 个答案:

答案 0 :(得分:2)

如果您使用ndiff,则可以使用restore

恢复原始字符串
>>> diff = difflib.ndiff(foo, bar)
>>> diff = list(diff)
>>> print ''.join(difflib.restore(diff, 2))
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed orci lobortis lectus bibendum baculo mihi. Integer iaculis eros porttitor velit porttitor scelerisque. Nunc venenatis nibh.
>>> ''.join(difflib.restore(diff, 2))==''.join(bar)
True
>>> ''.join(difflib.restore(diff, 1))==''.join(foo)
True
>>> 

请注意,因为统一差异通常会丢弃比较等于开头的行几乎不可能恢复到原始字符串。 ndiff显示了所有内容,因此可以直接恢复原始内容。