|
Size: 821
Comment:
|
Size: 2580
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 3: | Line 3: |
| '''`difflib`''' is a module for producing file deltas. |
|
| Line 9: | Line 11: |
| == HtmlDiff == {{{ from pathlib import Path import difflib old = Path('v1.txt') new = Path('v2.txt') out = Path('diff.txt') d = difflib.HtmlDiff(wrapcolumn=80).make_file(old, new, fromdesc='v1.txt', todesc='v2.txt') with open(out, "w") as f: |
== Usage == Consider an 'old' file like: {{{ foo bar baz 1 2 3 }}} And a 'new' file like: {{{ foo bar baz 1 5 3 }}} ---- === Context Diff === Use '''`context_diff`''' to create a delta in context diff format. To create a file like: {{{ *** a.txt --- b.txt *************** *** 1,9 **** foo bar - - baz 1 ! 2 3 --- 1,7 ---- foo bar baz 1 ! 5 3 }}} Try: {{{ import difflib with open('a.txt', 'r') as f: old = f.readlines() with open('b.txt', 'r') as f: new = f.readlines() d = difflib.context_diff(old, new, fromdesc='a.txt', todesc='b.txt') with open('diff.txt', "w") as f: f.writelines(d) }}} ---- === Differ === Use '''`ndiff`''' to create a delta in differ style. Try create a file like: {{{ foo bar - - baz 1 - 2 + 5 3 }}} Try: {{{ import difflib with open('a.txt', 'r') as f: old = f.readlines() with open('b.txt', 'r') as f: new = f.readlines() d = difflib.ndiff(old, new) with open('diff.txt', "w") as f: f.writelines(d) }}} ---- === Unified Diff === Use '''`unified_diff`''' to create a delta in unified diff format. To create a file like: {{{ --- a.txt +++ b.txt @@ -1,9 +1,7 @@ foo bar - - baz 1 -2 +5 3 }}} Try: {{{ import difflib with open('a.txt', 'r') as f: old = f.readlines() with open('b.txt', 'r') as f: new = f.readlines() d = difflib.unified_diff(old, new, fromdesc='a.txt', todesc='b.txt') with open('diff.txt', "w") as f: f.writelines(d) }}} ---- === HTML Table === To create a stylized HTML table with line-by-line comparisons, use the '''`HtmlDiff`''' class. {{{ import difflib with open('a.txt', 'r') as f: old = f.readlines() with open('b.txt', 'r') as f: new = f.readlines() d = difflib.HtmlDiff(wrapcolumn=80).make_file(old, new, fromdesc='a.txt', todesc='b.txt') with open('diff.html', "w") as f: |
| Line 28: | Line 192: |
| == Context_Diff == ---- == Get_Close_Matches == ---- == NDiff == ---- == Restore == ---- == Unified_Diff == {{{ from pathlib import Path import difflib old = Path('v1.txt') new = Path('v2.txt') d = difflib.unified_diff(old, new, fromfile='v1.txt', tofile='v2.txt') }}} |
== Functions == === Diff_Bytes === === Get_Close_Matches === === Restore === ---- == Classes == === Differ === === SequenceMatcher === |
| Line 71: | Line 218: |
| [[https://pymotw.com/3/difflib/|Python Module of the Day article for difflib]] |
Python DiffLib
difflib is a module for producing file deltas.
Contents
Usage
Consider an 'old' file like:
foo bar baz 1 2 3
And a 'new' file like:
foo bar baz 1 5 3
Context Diff
Use context_diff to create a delta in context diff format.
To create a file like:
*** a.txt --- b.txt *************** *** 1,9 **** foo bar - - baz 1 ! 2 3 --- 1,7 ---- foo bar baz 1 ! 5 3
Try:
import difflib
with open('a.txt', 'r') as f:
old = f.readlines()
with open('b.txt', 'r') as f:
new = f.readlines()
d = difflib.context_diff(old, new, fromdesc='a.txt', todesc='b.txt')
with open('diff.txt', "w") as f:
f.writelines(d)
Differ
Use ndiff to create a delta in differ style.
Try create a file like:
foo bar - - baz 1 - 2 + 5 3
Try:
import difflib
with open('a.txt', 'r') as f:
old = f.readlines()
with open('b.txt', 'r') as f:
new = f.readlines()
d = difflib.ndiff(old, new)
with open('diff.txt', "w") as f:
f.writelines(d)
Unified Diff
Use unified_diff to create a delta in unified diff format.
To create a file like:
--- a.txt +++ b.txt @@ -1,9 +1,7 @@ foo bar - - baz 1 -2 +5 3
Try:
import difflib
with open('a.txt', 'r') as f:
old = f.readlines()
with open('b.txt', 'r') as f:
new = f.readlines()
d = difflib.unified_diff(old, new, fromdesc='a.txt', todesc='b.txt')
with open('diff.txt', "w") as f:
f.writelines(d)
HTML Table
To create a stylized HTML table with line-by-line comparisons, use the HtmlDiff class.
import difflib
with open('a.txt', 'r') as f:
old = f.readlines()
with open('b.txt', 'r') as f:
new = f.readlines()
d = difflib.HtmlDiff(wrapcolumn=80).make_file(old, new, fromdesc='a.txt', todesc='b.txt')
with open('diff.html', "w") as f:
f.write(d)
Functions
Diff_Bytes
Get_Close_Matches
Restore
Classes
Differ
SequenceMatcher
See also
Python difflib module documentation
Python Module of the Day article for difflib
