File content Difference Comparison method
Directory:
- Example: Comparison of two string differences
- Generate aesthetically pleasing contrast HTML-formatted documents
- Compare Nginx configuration file differences
comparison of two string differences
This example uses the Difflib module to achieve the difference between the two strings and then output in a versioning style.
>>>ImportDifflib>>> >>> text1="Difflib Document v7.4" #定义字符串1 >>> text2="Difflib Document v7.5" #定义字符串2 >>> >>> text1_lines=text1.splitlines () #进行分割 for comparison >>> text2_lines=Text2.splitlines ()>>> >>> diff=Difflib. Differ () #创建Differ () object >>> >>> differ=Diff.compare (text1_lines,text2_lines) #采用compare方法对比字符串>>> >>>Print('\ n'. Join (list (differ)))-Difflib Document v7.4? ^+Difflib Document v7.5? ^>>>
//Comment: Description of the meaning of the symbol
//-#包含在第一个序列行中, but not included in the second sequence line
//+ #包含在第二个序列行中, but not included in the first sequence line
//Empty #两个序列行一致
//? #两个序列行存在增量差异
//^ #两个序列行存在差异字符
generate aesthetically pleasing contrast HTML-formatted documents
An aesthetically pleasing HTML document can be generated using the Make_file () method of the Htmldiff () class.
Import difflib>>> >>> text1_lines=text1.splitlines ()>>> text2_lines= Text2.splitlines ()>>> >>> >>> text1_lines=text1.splitlines ()>>> text2_lines=text2.splitlines ()>>> >>> diff=difflib. Htmldiff ()print(Diff.make_file (text1_lines,text2_lines))
#将生成的代码保存成 *.html Open with a browser to see the effect. Mei da
Compare nginx configuration file Differences
ImportDifflibImportSYSTry: Textfile1= Sys.argv[1] #第一个文件参数textfile2= Sys.argv[2] #第二个文件参数exceptException (E):Print("Error:"+str (e))Print("Usage:python sample3.py filename1 filename2") Sys.exit ()defReadFile (filename): #文件读取分隔函数Try: FileHandle= open (filename,'RB') Text=Filehandle.read (). Splitlines () #读取后进行分分隔filehandle.close ( )returntextexceptIOError as Error:Print('Read file Error:'+Str (Error)) Sys.exit () Text1_lines=ReadFile (textfile1) #调用readline函数, gets the delimited string text2_lines=ReadFile (textfile2) d=Difflib. Htmldiff () #创建htmldiff () object Print(D.make_file (str (text1_lines), str (text2_lines))) #通过make_file方法输出HTML格式的比对结果
//Comment: Call format python *.py nginx.conf nginx1.conf > index.html
How to compare file and directory differences
Directory:
- Module Common Method Description
- Example: Verify the difference from the backup directory
update Now, a little dirty, roll up the sleeves refueling dry!!!!
Send e-mail module smtplib
Directory:
- Common classes and methods of Smtplib modules
- Customizing personalized Message Format methods
- Customizing common message Format examples
Methods of detecting Web service quality
Directory:
- Module Common Method Description
- Example: Implementing the quality of probing Web services
"Python Automation operation and Maintenance Road" business Service Monitoring (II)