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
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.
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.
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:
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.
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)
Did you have a look at the built-in python module difflib? Have a look that this example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With