python-File Content Difference comparison

Source: Internet
Author: User
Through the Difflib module to achieve differences in file content, it is the standard library module python, does not need to be installed separately, the role is to compare the differences between files. and the output HTML document is supported, we can compare the difference between the code and the configuration file with Difflib.


1. Compare the differences of two strings

Print the results of the operation:

Understand what the output is:

"-" symbol: contained in the first sequence, but not in the second sequence;

The "+" symbol: contained in the second sequence, but not in the first sequence;

"" (i.e., empty): Two sequence lines are consistent;

"?" Symbol: Indicates that there is an incremental difference in two sequence rows;

"^" Symbol: marks the difference character that exists for two sequence lines.


1. Output HTML-formatted document (using the Make_file () method of the Htmldiff () class)

When we execute this script we export it to a file that ends in. html (Python check_str.py > Diff.html) and opens the view in the browser.


Small example: For example, when we maintain multiple profiles, we compare the differences between different versions of the configuration file, and we see more clearly the difference configuration items that are added or modified. The idea is to read the contents of the file, line as a segmentation, and then compare the generation of HTML documents (here the passwd file instead of the configuration file)




python-File Content Difference comparison

Related Article

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.