Python2.7-difflib

Source: Internet
Author: User

Difflib is primarily used to compare the differences of two sequences, common to string comparisons, and to generate reports on variances.

Three classes are defined within the module: Sequencematcher, differ, Htmldiff

Differ class initialization has two optional parameters Linejunk accepts a function that has only one string argument, represents the ignored string, Charjunk accepts a single character function, represents the ignored character, and there is only one method in the class compare (A, A, b), Two sequences must be separate strings with \ n Split, for example from File.readlines ().

Htmldiff class initialization has four optional parameters,Tabsize=8,wrapcolumn=none, Linejunk and Charjunk Ibid, two methods Make_file and make_table in the class ( Fromlines, Tolines [, fromdesc][, todesc][, context][, Numlines]), From/todesc represents the file header string, which is generally empty, Context and Numlines generally default, red do not understand.

Sequencematcher (Isjunk=none, a= ", b=", autojunk=true) Isjunk can accept a single sequence of functions. In-class method Set_seqs (A, B), set_seq1 (a), SET_SEQ2 (a),

Find_longest_match (Alo,ahi,blo,bhi): Find the longest match from A[alo:ahi] and B[blo:bhi]

Get_matching_blocks (): Returns a list of three elements, containing the first and last matches, and the third element is the length of a, b

Get_opcodes (): To obtain a transition from A to B, there are 4 methods (' Replace ', ' delete ', ' equal ', ' insert ')

Get_grouped_opcodes ([n]): Gets the generator that transforms the N-line method

Ratio (): A and B similarity, if not run get_matching_blocks () or get_opcodes (), this method is time consuming, the following two methods can be used instead of

Quick_ratio (): Fast calculation

Real_quick_ratio (): The fastest calculation, no

In-module methods:

Context_diff (A, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]): Compare A and B to return a generator in context_diff format

Ndiff (A, b[, linejunk][, Charjunk]): Compare A and B to return a generator in differ format

Unified_diff (A, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]): Compare A and B to return a generator in unified_diff format

Get_close_matches (Word, possibilities[, n][, Cutoff]): n is the maximum number returned, cutoff is the threshold, below its mismatch, returns the optimal match result, from large to small arrangement

Restore (sequence, which): pushes the sequence generated from Ndiff or Differ.compare () back to the original sequence, with a which value of 1 or 2, representing the first/two sequences

Python2.7-difflib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.