Skip to content Skip to sidebar Skip to footer

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 tha

Solution 1:

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)

Solution 2:

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

Post a Comment for "How To Print The Comparison Of Two Multiline Strings In Unified Diff Format?"