How to print the comparison of two multiline strings in unified diff format?
Asked Answered
J

2

23

Do you know any library that will help doing that?

I would write a function that prints the differences between two multiline strings in the unified diff format. Something like that:

def print_differences(string1, string2):
    """
    Prints the comparison of string1 to string2 as unified diff format.
    """
    ???

An usage example is the following:

string1="""
Usage: trash-empty [days]

Purge trashed files.

Options:
  --version   show program's version number and exit
  -h, --help  show this help message and exit
"""

string2="""
Usage: trash-empty [days]

Empty the trash can.

Options:
  --version   show program's version number and exit
  -h, --help  show this help message and exit

Report bugs to http://code.google.com/p/trash-cli/issues
"""

print_differences(string1, string2)

This should print something like that:

--- string1 
+++ string2 
@@ -1,6 +1,6 @@
 Usage: trash-empty [days]

-Purge trashed files.
+Empty the trash can.

 Options:
   --version   show program's version number and exit
Jumbled answered 10/5, 2009 at 12:51 Comment(0)
J
32

This is how I solved:

def _unidiff_output(expected, actual):
    """
    Helper function. Returns a string containing the unified diff of two multiline strings.
    """

    import difflib
    expected=expected.splitlines(1)
    actual=actual.splitlines(1)

    diff=difflib.unified_diff(expected, actual)

    return ''.join(diff)
Jumbled answered 10/5, 2009 at 14:25 Comment(1)
This works except in the case where the the diff is a blank line. "hello\nthere" != "hello\nthere\n". Using expected = [line for line in expected.split('\n')] instead will solve this.Curtiscurtiss
I
26

Did you have a look at the built-in python module difflib? Have a look that this example

Illtimed answered 10/5, 2009 at 12:54 Comment(1)
that linked example is perfect.Finkle

© 2022 - 2024 — McMap. All rights reserved.