繁体   English   中英

在Python中反向diff -u

[英]Reverse diff -u in python

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

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

谁能向我解释说真的很理解这种统一格式吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM