1. 程式人生 > >兩行資料進行對比-python

兩行資料進行對比-python

#!/usr/bin/python
import difflib
text1 = """text1:  #定義字串1
This module provides classes and functions for comparing sequences.
including HTML and context and unified diffs."""

text1_lines = text1.splitlines() #以行進行分隔,以便進行對比
text2 = """text2: #定義第二個字串
This module provides """

text2_lines = text2.splitlines()
d = difflib.Differ() #建立Differ物件
diff = d.compare(text1_lines, text2_lines)  #採用compare方法對字串進行比較
print('\n'.join(list(diff)))