我需要以unix统一的diff-like样式显示每行的字符差异。有没有一种方法可以使用difflib做到这一点?在
我可以得到“统一的差异”和“字符每行差异”分别使用difflib.unified_diff以及差异。不同()(ndiff),但我如何将它们组合起来呢?在
这就是我要找的:
#
# This is difflib.unified
#
>>> print ''.join(difflib.unified_diff('one\ntwo\nthree\n'.splitlines(1), 'ore\ntree\nemu\n'.splitlines(1), 'old', 'new'))
--- old
+++ new
@@ -1,3 +1,3 @@
-one
-two
-three
+ore
+tree
+emu
>>>
#
# This is difflib.Differ
#
>>> print ''.join(difflib.ndiff('one\ntwo\nthree\n'.splitlines(1), 'ore\ntree\nemu\n'.splitlines(1))),
- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu
>>>
#
# I want the merge of above two, something like this...
#
>>> print ''.join(unified_with_ndiff('one\ntwo\nthree\n'.splitlines(1), 'ore\ntree\nemu\n'.splitlines(1))),
--- old
+++ new
@@ -1,3 +1,3 @@
- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu
>>>
在深入研究difflib的源代码后,我自己找到了答案。在
相关问题 更多 >
编程相关推荐