Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6671305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:22:21+00:00 2026-05-26T03:22:21+00:00

I am using difflib.HtmlDiff to compare two files. I want the differences to be

  • 0

I am using difflib.HtmlDiff to compare two files. I want the differences to be highlighted in the outputted html.

This already works when there are a maximum of two different chars in one line:

a = "2.000"
b = "2.120"

But when there are more different characters on one line then in the output the whole line is marked red (on the left side) or green (on the right side of the table):

a = "2.000"
b = "2.123"

Is this behaviour configurable? So can I set the number of different characters at which the line is marked as deleted / added?

EDIT:

Example:

import difflib
diff=difflib.HtmlDiff()
print(diff.make_file(
'''
2.000
2.000
2.000
'''.splitlines(),
'''
2.001
2.010
2.011
'''.splitlines()))

Gives me this output:

output

Line 2 is the output I want. It highlights the differences in yellow.
Line 3 is odd for me because it does not detect the one character change but instead shows it as delete / add.
Line 4 same as for line 3 but the whole line is marked.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:22:22+00:00Added an answer on May 26, 2026 at 3:22 am

    difflib’s algorithm does not claim to yield minimal edit sequences.
    Although that statement comes from the docs for SequenceMatcher, I suspect it applies to difflib in general, and HTMLDiff in particular.

    While googling around for “python alternative difflib minimal edit”
    I found google-diff-match-patch. If you try out their demo for Diff
    with your example strings, it yields

    enter image description here

    Although the output is not exactly what you requested, it does show that it found the minimal edits.

    The API docs state

    diff_prettyHtml(diffs) => html

    Takes a diff array and returns a pretty HTML sequence. This
    function is mainly intended as an example from which to write ones own
    display functions.

    which suggests looking at the source code for diff_prettyHtml might be a good starting point from which to build the HTML table you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using difflib to compare files in two directories (versions from consecutive years).
I am using the difflib.HtmlDiff class, calling the function using two sets of text
I am trying to use Difflib.SequenceMatcher to compute the similarities between two files. These
Using Facebox with .NET (Web Forms) is painful--this primarily HTML site was designed by
Using C#, I want to format a decimal to only display two decimal places
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I'd like to align two lists in a similar way to what difflib.Differ would
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.