1

unix diff -u コマンドの出力と、その出力の作成に使用されたファイルの 1 つを取得し、2 番目のファイルを出力するモジュールを Python で作成する必要があります。

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 に答える