生成美觀的對比HTML格式文件
阿新 • • 發佈:2018-12-30
採用HtmlDIff()類的make_file()方法就可以生存美觀的HTML文件。示例:
d = difflib.Differ() #建立Differ()物件
diff = d.compare(text1_lines,text2_lines) #採用compare方法對字串進行比較
print('\n'.join(list(diff)))
替換成:
d = difflib.HtmlDiff() #建立HtmlDiffer()物件
print(d.make_file(text1_lines,text2_lines)) #採用make_file方法對字串進行比較
#simple2.py程式碼(修改後的程式碼)
#!/usr/bin/env python import difflib text1 = """text1: #定義字串1 This module provides classes and functions for comparing sequences. including HTML and context and unified diffs. difflib document v7.4 add string""" text1_lines = text1.splitlines() #以行進行分割,以便進行對比 text2= """text2: #定義字串2 This module provides classes and functions for comparing sequences. including HTML and context and unified diffs. difflib document v7.5 add string""" text2_lines = text2.splitlines() d = difflib.HtmlDiff() #建立HtmlDiffer()物件 print(d.make_file(text1_lines,text2_lines)) #採用make_file方法對字串進行比較
執行python3 simple2.py > diff.html,在使用瀏覽器開啟diff.html檔案,如下圖所示,HTML文件包括了行號、差異標誌、圖例等資訊,可讀性增強了很多。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} </style> </head> <body> <table class="diff" id="difflib_chg_to0__top" cellspacing="0" cellpadding="0" rules="groups" > <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <tbody> <tr><td class="diff_next" id="difflib_chg_to0__1"><a href="#difflib_chg_to0__1">n</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap"><span class="diff_sub">text1: #定義字串1</span></td><td class="diff_next"><a href="#difflib_chg_to0__1">n</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap"><span class="diff_add">text2: #定義字串2</span></td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap">This module provides classes and functions for comparing sequences.</td><td class="diff_next"></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap">This module provides classes and functions for comparing sequences.</td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap">including HTML and context and unified diffs.</td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap">including HTML and context and unified diffs.</td></tr> <tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_4">4</td><td nowrap="nowrap">difflib document v7.<span class="diff_chg">4</span></td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_4">4</td><td nowrap="nowrap">difflib document v7.<span class="diff_chg">5</span></td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_5">5</td><td nowrap="nowrap">add string</td><td class="diff_next"></td><td class="diff_header" id="to0_5">5</td><td nowrap="nowrap">add string</td></tr> </tbody> </table> <table class="diff" summary="Legends"> <tr> <th colspan="2"> Legends </th> </tr> <tr> <td> <table border="" summary="Colors"> <tr><th> Colors </th> </tr> <tr><td class="diff_add"> Added </td></tr> <tr><td class="diff_chg">Changed</td> </tr> <tr><td class="diff_sub">Deleted</td> </tr> </table></td> <td> <table border="" summary="Links"> <tr><th colspan="2"> Links </th> </tr> <tr><td>(f)irst change</td> </tr> <tr><td>(n)ext change</td> </tr> <tr><td>(t)op</td> </tr> </table></td> </tr> </table> </body> </html>生成的diff.html程式碼
在瀏覽器中帶開diff.html程式碼檔案: