4

因此,使用 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

4

1 回答 1

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 显示所有内容,因此可以直接恢复原始内容。

于 2012-04-05T18:02:21.417 回答