Weekly Subscription $1.99 USD per week until cancelled Monthly Subscription $4.99 USD per month until cancelled Annual Subscription $29.99 USD per year until cancelled. Jandy 7305 180-Degree, 2-Inch to 2-1/2-Inch Check Valve. 4.7 out of 5 stars 221. 83 $54.80 $54.80. Get it as soon as Fri, Sep 18. FREE Shipping by Amazon. More Buying Choices $38.76 (15 used & new offers) Pentair 263042 Check Valve CPVC for Pool Pumps, 2 Port Straight.
Navigation
- modules |
- next |
- previous |
- PyMOTW »
- String Services »
Purpose: | Compare sequences, especially lines of text. |
---|---|
Available In: | 2.1 and later |
The difflib module contains tools for computing and workingwith differences between sequences. It is especially useful forcomparing text, and includes functions that produce reports usingseveral common difference formats.
The examples below will all use this common test data in thedifflib_data.py module:
Comparing Bodies of Text¶
The Differ class works on sequences of text lines andproduces human-readable deltas, or change instructions, includingdifferences within individual lines. Autocad 2013 serial number and product key free.
The default output produced by Differ is similar to thediff command line tool is simple with the Differclass. It includes the original input values from both lists,including common values, and markup data to indicate what changes weremade.
- Lines prefixed with - indicate that they were in the firstsequence, but not the second.
- Lines prefixed with + were in the second sequence, but not thefirst.
- If a line has an incremental difference between versions, an extraline prefixed with ? is used to highlight the change within thenew version.
- If a line has not changed, it is printed with an extra blank spaceon the left column so that it it lines up with the other lines thatmay have differences.
To compare text, break it up into a sequence of individual lines andpass the sequences to compare().
The beginning of both text segments in the sample data is the same, sothe first line is printed without any extra annotation. Nfs mount autofs.
The second line of the data has been changed to include a comma in themodified text. Both versions of the line are printed, with the extrainformation on line 4 showing the column where the text was modified,including the fact that the , character was added.
Lines 6-9 of the output shows where an extra space was removed.
Next a more complex change was made, replacing several words in a phrase.
The last sentence in the paragraph was changed significantly, so thedifference is represented by simply removing the old version and adding thenew (lines 20-23).
The ndiff() Uplink 1 0 0 3 download free. function produces essentially the same output.
The processing is specifically tailored for working with text data andeliminating 'noise' in the input.
Other Output Formats¶
While the Differ class shows all of the input lines, aunified diff only includes modified lines and a bit of context. InPython 2.3, the unified_diff() function was added to producethis sort of output:
The output should look familiar to users of subversion or otherversion control tools: Apple support app.
Using context_diff() produces similar readable output:
HTML Output¶
HtmlDiff produces HTML output with the same information asDiff.
This example uses make_table(), which only returns thetable tag containing the difference information. Themake_file() method produces a fully-formed HTML file as output.
Note
The output is not included here because it is very verbose.
Junk Data¶
All of the functions that produce difference sequences acceptarguments to indicate which lines should be ignored, and whichcharacters within a line should be ignored. These parameters can beused to skip over markup or whitespace changes in two versions of afile, for example.
The default for Differ is to not ignore any lines orcharacters explicitly, but to rely on the ability ofSequenceMatcher to detect noise. The default forndiff() is to ignore space and tab characters.
Diff Checker 2 1 4 234 X 24
Comparing Arbitrary Types¶
Diff Checker 2 1 4 234 X 27
The SequenceMatcher class compares two sequences of anytypes, as long as the values are hashable. It uses an algorithm toidentify the longest contiguous matching blocks from the sequences,eliminating 'junk' values that do not contribute to the real data.
This example compares two lists of integers and usesget_opcodes() to derive the instructions for converting theoriginal list into the newer version. The modifications are appliedin reverse order so that the list indexes remain accurate after itemsare added and removed.
SequenceMatcher works with custom classes, as well asbuilt-in types, as long as they are hashable.
See also
- difflib
- The standard library documentation for this module.
- Pattern Matching: The Gestalt Approach
- Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener published in Dr. Dobb's Journal in July, 1988.
Navigation
Diff Checker 2 1 4 234 X 22
- modules |
- next |
- previous |
- PyMOTW »
- String Services »