PyDocs

Nav

Text Processing Services

unified_diff

difflib

function (source)
  • unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', *, color=False)
Compare two sequences of lines; generate the delta as a unified diff.

Unified diffs are a compact way of showing line changes and a few
lines of context.  The number of context lines is set by 'n' which
defaults to three.

By default, the diff control lines (those with ---, +++, or @@) are
created with a trailing newline.  This is helpful so that inputs
created from file.readlines() result in diffs that are suitable for
file.writelines() since both the inputs and outputs have trailing
newlines.

For inputs that do not have trailing newlines, set the lineterm
argument to "" so that the output will be uniformly newline free.

Set 'color' to True to enable output in color, similar to
'git diff --color'. Even if enabled, it can be
controlled using environment variables such as 'NO_COLOR'.

The unidiff format normally has a header for filenames and modification
times.  Any or all of these may be specified using strings for
'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
The modification times are normally expressed in the ISO 8601 format.

Example:

>>> for line in unified_diff('one two three four'.split(),
...             'zero one tree four'.split(), 'Original', 'Current',
...             '2005-01-26 23:30:50', '2010-04-02 10:20:52',
...             lineterm=''):
...     print(line)                 # doctest: +NORMALIZE_WHITESPACE
--- Original        2005-01-26 23:30:50
+++ Current         2010-04-02 10:20:52
@@ -1,4 +1,4 @@
+zero
 one
-two
-three
+tree
 four
Changed in 3.15
  • 3.15: Added the color parameter.
1 Example
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> sys.stdout.writelines(unified_diff(s1, s2, fromfile='before.py', tofile='after.py'))
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido
from the Python docs
See Also
No see-alsos for difflib/unified_diff
0 Notes
No notes for unified_diff