我需要在python中编写一个模块,它获取unix diff -u命令的输出,以及用于创建输出的文件之一,并返回输出第二个文件。
diff -u以统一格式返回diff文件
有人可以向我解释真的要理解统一格式吗?
答案 0 :(得分:0)
Google python port库有diff-match-patch个:
使用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