<p>你知道有什么图书馆能帮你吗?</p>
<p>我将编写一个函数,以统一的diff格式打印两个多行字符串之间的差异。像这样的:</p>
<pre><code>def print_differences(string1, string2):
"""
Prints the comparison of string1 to string2 as unified diff format.
"""
???
</code></pre>
<p>使用示例如下:</p>
<pre><code>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)
</code></pre>
<p>这应该会打印出类似的内容:</p>
<pre><code>--- 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
</code></pre>