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

前端 未结 2 1273
耶瑟儿~
耶瑟儿~ 2021-02-12 12:41

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. Somethi

相关标签:
2条回答
  • 2021-02-12 13:26

    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)
    
    0 讨论(0)
  • 2021-02-12 13:38

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

    0 讨论(0)
提交回复
热议问题