原创

3.3.5 使用HtmlDiff对象

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://mysoft.blog.csdn.net/article/details/48765817

当想创建一个HTML来表示文件之间差异时,就可以使用HtmlDiff对象。特别对于Web服务器的应用就更加方便了,直接把数据返回即可。

例子:

#python3.4.3

 

import difflib

 

text1 = '''  1. Beautiful is better than ugly.

   2. Explicit is better than implicit.

   3. Simple is better than complex.

   4. Complex is better than complicated.

 '''.splitlines(keepends=True)

 

text2 = '''  1. Beautiful is better than ugly.

   3.   Simple is better than complex.

   4. Complicated is better than complex.

   5. Flat is better than nested.

 '''.splitlines(keepends=True)

 

diff = difflib.HtmlDiff().make_file(text1, text2)

print(diff)

结果输出如下:

<!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=ISO-8859-1" />

    <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__0"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap">  1. Beautiful is better than ugly.</td><td class="diff_next"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap">  1. Beautiful is better than ugly.</td></tr>

            <tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap"><span class="diff_sub">   2. Explicit is better than implicit.</span></td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header"></td><td nowrap="nowrap"></td></tr>

            <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap">   3. Simple is better than complex.</td><td class="diff_next"></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap">   3.<span class="diff_add">  </span> Simple is better than complex.</td></tr>

            <tr><td class="diff_next"></td><td class="diff_header" id="from0_4">4</td><td nowrap="nowrap"><span class="diff_sub">   4. Complex is better than complicated.</span></td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap"><span class="diff_add">   4. Complicated is better than complex.</span></td></tr>

            <tr><td class="diff_next"></td><td class="diff_header"></td><td nowrap="nowrap"></td><td class="diff_next"></td><td class="diff_header" id="to0_4">4</td><td nowrap="nowrap"><span class="diff_add">   5. Flat is better than nested.</span></td></tr>

            <tr><td class="diff_next"></td><td class="diff_header" id="from0_5">5</td><td nowrap="nowrap"> </td><td class="diff_next"></td><td class="diff_header" id="to0_5">5</td><td nowrap="nowrap"> </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>

把这段HTML文本写到一个文件里,使用浏览器打开会显示如下:

 

 



蔡军生 微信号:shenzhencai  深圳

文章最后发布于: 2015-09-27 11:29:37
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览