Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 732
Andrea Francia Avatar asked May 10 '09 12:05

Andrea Francia


People also ask

What is the best way to compare two strings of char?

Take the longest, compare each char one by one, add char to a string if it exists or * if not.. Please Sign up or sign in to vote. With LINQ you can use EXCEPT. Make two enumarables from each string (or 2 arrays of char) compare those with EXCEPT.

What is the operator compare if the 2 string are same?

The is operator compare if the 2 string are the same instance. In Python—and in many other languages—we say two objects are the same instance if they are the same object in memory.

How to create multiline strings in Java?

In Java, it's very easy to get the operating system line separator: String newLine = System.getProperty ( "line.separator" ); We're going to use this newLine in the following sections to create multiline strings. 4. String Concatenation String concatenation is an easy native method that can be used to create multiline strings:

How to compare if two strings are equal in Python?

Another way of comparing if two strings are equal in Python is using the is operator. However, the kind of comparison it performs is different than ==. The is operator compare if the 2 string are the same instance. In Python—and in many other languages—we say two objects are the same instance if they are the same object in memory.


2 Answers

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)
like image 183
Andrea Francia Avatar answered Sep 28 '22 05:09

Andrea Francia


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

like image 43
Nadia Alramli Avatar answered Sep 28 '22 07:09

Nadia Alramli