Python automatic operation _ file content difference comparative analysis

  • 2020-06-15 09:44:03
  • OfStack

Module: difflib

Installation: Python version 2.3 or greater comes with the system

What it does: It compares the differences between texts and supports output of a readable HTML document, similar to the diff command in Linux.

Comparison of the differences between two strings:


#import difflib
 
#text1='''
 
#hello world.
 
#how are you.
 
#nice to meet you.
 
#'''
 
#text1_lines=text1.splitlines() #  Split in rows to facilitate comparison 
 
#text2='''
 
#Hello World.
 
#how are you!
 
#Nice to meet you~
 
#'''
 
#text2_lines=text2.splitlines() #  Split in rows to facilitate comparison 
 
#dif1=difflib.Differ() #  create Differ()  object 
 
#diff1=dif1.compare(text1_lines,text2_lines) #  using compare()  Method to compare strings 
 
#print('\n'.join(list(diff1)))

In addition, difflib has the SequenceMatcher() class, which supports the comparison of sequences of any type; There is also the HtmlDiff() class, which supports output of the comparison results in HTML format.

Description of symbolic meaning:

symbol

meaning

'-'

Included in the first sequence line, but not included in the second sequence line

'+'

Included in the second sequence line, but not included in the first sequence line

' '

The two sequences are in line 1

'?'

Indicates an incremental difference between two sequence lines

'^'

Marks the difference character that exists between two sequence lines

Generate HTML format documents:


#import difflib
#text1='''
 
#hello world.
 
#how are you.
 
#nice to meet you.
 
#'''
 
#text1_lines=text1.splitlines() #  Split in rows to facilitate comparison 
 
#text2='''
 
#Hello World.
 
#how are you!
 
#Nice to meet you~
 
#'''
 
#text2_lines=text2.splitlines() #  Split in rows to facilitate comparison 
 
#d=difflib.HtmlDiff()
 
#print(d.make_file(text1_lines,text2_lines))
 
#  Then make the generated file become  .html You can view it in a browser 

Comparison of configuration file differences:


#import difflib
 
#import os
 
#try:
 
# textfile1=sys.argv[1] #  The first 1 Three configuration file path parameters 
 
# textfile2=sys.argv[2] #  The first 2 Three configuration file path parameters 
 
#except Exception,e:
 
# print('Error:'+str(e))
 
# print(' Use: Script name .py filename1 filename2')
 
# sys.exit()
 
#def readfile(filename): #  File read delimiter function 
 
# try:
 
# fileHandle=open(filename,'rb')
 
# text=fileHandle.read().splitlines() #  After reading, separate by row 
 
# fileHandle.close()
 
# return text
 
# except IOError as error:
 
# print(' Error reading file: '+str(error))
 
# sys.exit()
 
#if textfile1=='or textfile2==':
 
# print(' Use: Script name .py filename1 filename2')
 
# sys.exit()
 
#text1_lines=readfile(textfile1) #  call readfile Gets the delimited string 
 
#text2_lines=readfile(textfile2)
 
#d=difflib.HtmlDiff() #  create HtmlDiff()  Class object 
 
#print(d.make_file(text1_lines,text2_lines)) #  through make_file()  Methods the output HTML Formatted result 

Related articles: