/ / Reverse diff -u en python - python, unified-diff

Reverse diff -u en python - python, unified-diff

J'ai besoin d'écrire un module en python qui récupère le résultat d'une commande unix diff -u et l'un des fichiers utilisés pour créer cette sortie, puis renvoie le second fichier.

Le diff -u retourne un fichier diff dans un format unifié

Quelqu'un pourrait-il m'expliquer vraiment comment comprendre ce format unifié?

Réponses:

0 pour la réponse № 1

Il y a un port python de Google "s diff-match-patch bibliothèque:

Installez-le avec pip:

pip install diff-match-patch

Exemple d'application d'un correctif à partir de l'interpréteur 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