/ / Umgekehrter Diff -u in Python - Python, Unified-Diff

Umgekehrtes Diff -u in Python - Python, Unified-Diff

Ich muss ein Modul in Python schreiben, das die Ausgabe eines Unix-Befehls diff -u und einer der Dateien abruft, die zum Erstellen dieser Ausgabe und zur Ausgabe der zweiten Datei verwendet wurden.

Das diff -u gibt eine diff-Datei in einem einheitlichen Format zurück

Könnte mir irgendjemand wirklich erklären, wie man dieses vereinheitlichte Format versteht?

Antworten:

0 für die Antwort № 1

Da ist ein Python-Port von Google "s diff-match-patch Bibliothek:

Installiere es mit pip:

pip install diff-match-patch

Ein Beispiel für das Anwenden eines Patches aus dem Python-Interpreter:

>>> 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