我需要在python中编写一个模块,该模块获取unix diff -u命令的输出以及用于创建该输出并返回输出第二个文件的文件之一。反向diff -u在python中
-u返回一个差异文件统一格式
任何人都可以向我解释,真正锄了解,统一格式的差异?
我需要在python中编写一个模块,该模块获取unix diff -u命令的输出以及用于创建该输出并返回输出第二个文件的文件之一。反向diff -u在python中
-u返回一个差异文件统一格式
任何人都可以向我解释,真正锄了解,统一格式的差异?
有谷歌的diff-match-patch库的python port:
与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
这不完全是补丁,我在做。我需要获取diff文件并提取另一个。此代码是否可以同时工作? –
我想我没有很好的理解你想完成什么。由于解释不够,你将不得不张贴你正在寻找的确切例子。 –
我明白了。基本上来说:我们用diff -u file_a和file_b创建一个diff文件。然后,我们给程序diff文件,以及文件a或b中的任一个,并且返回另一个作为输出 –
你可以看到[点击这里](http://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html#Unified-Format)也有一个例子,从GNU文档 –
你不能使用'patch'命令是因为...? – tripleee
因为这是一个练习,我需要自己做 –