1

我需要在 python 中编写一个模块,该模块获取 unix diff -u 命令的输出以及用于创建该输出的文件之一,并返回输出第二个文件。

diff -u 返回统一格式的diff文件

任何人都可以向我解释真正要了解这种统一格式吗?

4

1 回答 1

0

Google 的diff-match-patch库有一个python 端口:

使用 pip 安装它:

pip install diff-match-patch

从 python 解释器应用补丁的示例:

>>> from diff_match_patch import diff_match_patch
>>> old_version = '''#
... # Mac OS X Notice
... #
... # This file is not used by the host name and address resolution
... # or the DNS query routing mechanisms used by most processes on
... # this Mac OS X system.
... #
... # This file is automatically generated.
... #
... nameserver 192.168.1.1
... nameserver 8.8.8.8'''
>>> patch='''@@ -8,4 +8,4 @@
...  # This file is automatically generated.
...  #
...  nameserver 192.168.1.1
... -nameserver 8.8.8.8
... +nameserver 8.8.4.4'''
>>> patchobj = diff_match_patch()
>>> patches = patchobj.patch_fromText(patch)
>>> patched_version, results = patchobj.patch_apply(patches, old_version)
>>> print str(patched_version)
#
# Mac OS X Notice
#
# This file is not used by the host name and address resolution
# or the DNS query routing mechanisms used by most processes on
# this Mac OS X system.
#
# This file is automatically generated.
#
nameserver 192.168.1.1
nameserver 8.8.4.4
于 2015-08-30T11:25:45.887 回答