Added vs Deleted vs Changed - Python Difflib/Diff

Viewed 46

I created 2 files (below) and compared them with difflib.make_file.

In the HTML file generated, why do lines 1-3 show the individual changes in each line, but line 4 shows it's being deleted from diff_text1 and added in diff_text2?

(adding unnecessary detail to get past the detail/code ratio requirement.)

diff_text1:

The world is a big place.
The galaxy is a big place.
The planet is a big place.
This is in file1 and not file2.

diff_text2:

This world is a large place.
This galaxy is a large place.
This planet is a large place.
This is in file2 and not file1.

Code:

with open("diff_text1.txt", "r") as d1:
    diff_text1 = d1.readlines()
    
with open("diff_text2.txt", "r") as d2:
    diff_text2 = d2.readlines()
    

result4 = h.make_file(diff_text1, diff_text2)
print(result4)

Output 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_to14__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_to14__0"><a href="#difflib_chg_to14__top">t</a></td><td class="diff_header" id="from14_1">1</td><td nowrap="nowrap">Th<span class="diff_chg">e</span>&nbsp;world&nbsp;is&nbsp;a&nbsp;<span class="diff_chg">bi</span>g&nbsp;place.</td><td class="diff_next"><a href="#difflib_chg_to14__top">t</a></td><td class="diff_header" id="to14_1">1</td><td nowrap="nowrap">Th<span class="diff_chg">is</span>&nbsp;world&nbsp;is&nbsp;a&nbsp;<span class="diff_chg">lar</span>g<span class="diff_add">e</span>&nbsp;place.</td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from14_2">2</td><td nowrap="nowrap">Th<span class="diff_chg">e</span>&nbsp;galaxy&nbsp;is&nbsp;a&nbsp;<span class="diff_chg">bi</span>g&nbsp;place.</td><td class="diff_next"></td><td class="diff_header" id="to14_2">2</td><td nowrap="nowrap">Th<span class="diff_chg">is</span>&nbsp;galaxy&nbsp;is&nbsp;a&nbsp;<span class="diff_chg">lar</span>g<span class="diff_add">e</span>&nbsp;place.</td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from14_3">3</td><td nowrap="nowrap">Th<span class="diff_chg">e</span>&nbsp;planet&nbsp;is&nbsp;a&nbsp;<span class="diff_chg">bi</span>g&nbsp;place.</td><td class="diff_next"></td><td class="diff_header" id="to14_3">3</td><td nowrap="nowrap">Th<span class="diff_chg">is</span>&nbsp;planet&nbsp;is&nbsp;a&nbsp;<span class="diff_chg">lar</span>g<span class="diff_add">e</span>&nbsp;place.</td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from14_4">4</td><td nowrap="nowrap"><span class="diff_sub">This&nbsp;is&nbsp;in&nbsp;file1&nbsp;and&nbsp;not&nbsp;file2.</span></td><td class="diff_next"></td><td class="diff_header" id="to14_4">4</td><td nowrap="nowrap"><span class="diff_add">This&nbsp;is&nbsp;in&nbsp;file2&nbsp;and&nbsp;not&nbsp;file1.</span></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">&nbsp;Added&nbsp;</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>
0 Answers
Related