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

  • SEARCH
  • Home
  • 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 776887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:31:20+00:00 2026-05-14T19:31:20+00:00

I want to generate a diff between to revisions of text (more specifically, Markdown-formatted

  • 0

I want to generate a diff between to revisions of text (more specifically, Markdown-formatted articles) in Python.

I want to format this diff in a manner similar to what Github does.

I’ve looked at difflib and have found that it does what I want. However, the Differ class is too high-level; I would have to parse the diff lines to generate HTML with inline diffs. The Differ class uses the SequenceMatcher class to generate its diffs. But looking at the SequenceMatcher it’s very low-level in comparison. I haven’t even figured out how to do a line-by-line diff (I’ll admit I haven’t spent a lot of time experimenting).

Does anyone know of any resources for using the SequenceMatcher class (besides the difflib documentation)?

  • 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-14T19:31:21+00:00Added an answer on May 14, 2026 at 7:31 pm

    SequenceMatcher is actually not that low-level. The most interesting method for you is get_grouped_opcodes. It will return a generator, which generates lists with change descriptions.

    I’ll explain it on an example from a random commit on GitHub. Let’s say you run SequenceMatcher(None, a, b).get_grouped_opcodes() on the old and new file “tabs_events.js”. The generator will generate two groups, which are represent by those “…” lines in GitHub. It’s basically a group of changes. In each of the groups, you have a list of detailed changes stored as tuples. For the first group, it returns two changes that look like this (the first item is a change type, the next two numbers represent a line range to be removed, followed a line range to be added):

    ('replace', 24, 29, 24, 29)
    ('insert', 33, 33, 33, 35)
    

    The first one tell you to replace lines 24-28 (starting with 0) from the old file with lines 24-28 from the new file. The second one tells you to insert lines 33-34 from the new file on line 33 in the old file. I think it’s clear what would 'delete' do and 'equal' are those lines that are not highlighted in GitHub.

    If you don’t mind reading source code, take a look at the implementation of difflib.unified_diff(). It’s quite simple and it generates a plain-text equivalent of what you want.

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

Sidebar

Related Questions

I want to generate a list of which files changed between two revisions in
I want to generate a 16-bit executable BINARY RAW format by Watcom C compiler
I do something like svn diff > /mystuff/current.diff. I want to view this .diff
I want to generate a random floating point number between 0.000000000000000 and 0.999999999999999 .
I want to generate unique hexadecimal numbers in SQL. How can I do this?
I want to generate the random numbers using this loop. When i runs the
Is there an 'out-of-the-box' way in python to generate a list of differences between
Let's say I have a class of 30 students and want generate every possible
I want to generate an array with 10 elements (0, 1, 2, ..., 9)
I want to generate bytes sequence containing string length and string content. For example,

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.