Python Glossary Русский

all Python 3difflibfunction

difflib.ndiff()

diff formats

A human-oriented line-by-line comparison: '- ' and '+ ' for differing lines and a '? ' line pointing at changed characters.

Syntax

difflib.ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK)

Unlike unified_diff it outputs every line, including the equal ones (prefixed with two spaces), so the output of large files is huge. The '? ' lines highlight characters with ^, + and -, and the result is reversible: restore() gives back either input list. Lines should end in '\n' or the hints run together.

Examples

import difflib
for line in difflib.ndiff(['one\n', 'abcde\n'], ['one\n', 'abXde\n']):
    print(line, end='')  # → one / - abcde / ?   ^ / + abXde

See also

Open in the full glossarydocs.python.orgReport an inaccuracy