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 7759423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:37:31+00:00 2026-06-01T13:37:31+00:00

So, using Python’s difflib , I can generate a diff of two strings: foo

  • 0

So, using Python’s difflib, I can generate a diff of two strings:

foo = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Nullam sed orci lobortis lectus bibendum vehicula.\n Integer iaculis eros porttitor velit porttitor scelerisque.\n Nunc venenatis nibh.'.splitlines()
bar = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Mauris placerat, odio ut viverra gravida, elit leo tincidunt nisi, a pellentesque velit arcu id magna.\n Integer iaculis eros porttitor velit porttitor scelerisque.\n Nunc venenatis nibh.'.splitlines()

diff = difflib.unified_diff(foo, bar, lineterm='')

We can then check out the diff if we so desired:

>>> print '\n'.join(diff)
--- 
+++ 
@@ -1,4 +1,4 @@
 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- Nullam sed orci lobortis lectus bibendum vehicula.
+ Mauris placerat, odio ut viverra gravida, elit leo tincidunt nisi, a pellentesque velit arcu id magna.
  Integer iaculis eros porttitor velit porttitor scelerisque.
  Nunc venenatis nibh.

And here’s where I’m stuck. How can I apply that diff to string foo to result in string bar?

  • 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-06-01T13:37:32+00:00Added an answer on June 1, 2026 at 1:37 pm

    If you use ndiff then you can restore the original string using restore

    >>> diff = difflib.ndiff(foo, bar)
    >>> diff = list(diff)
    >>> print ''.join(difflib.restore(diff, 2))
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed orci lobortis lectus bibendum baculo mihi. Integer iaculis eros porttitor velit porttitor scelerisque. Nunc venenatis nibh.
    >>> ''.join(difflib.restore(diff, 2))==''.join(bar)
    True
    >>> ''.join(difflib.restore(diff, 1))==''.join(foo)
    True
    >>> 
    

    Please note since unified diffs usually drops lines that compared equal to begin with it would be almost impossible to restore to the original string. ndiff shows everything, so it would be straight forward to restore the original.

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

Sidebar

Related Questions

Using python and wsgiref.handlers, I can get a single variable from a form with
Using Python... How can I select all of the Sundays (or any day for
Using Python... How can I get a list of the days in a specific
Using Python regular expressions how can you get a True / False returned? All
When using Python is it possible that a dict can have a value that
Using Python, how can information such as CPU usage, memory usage (free, used, etc),
Using Python, I'm trying to read a list or strings backwards. When finding the
Using python 2.6.5, I can use the with statement without calling from __future__ import
Using Python I can test my code in the terminal / command line by
Using Python I want to be able to draw text at different angles using

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.