1、文件差异对比代码:
import difflib
text1 = ''' 1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
'''.splitlines(keepends=False) 按行分割,不保留空格,生成列表
text2 = ''' 1. Beautiful is better than ugly.
3. Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.
'''.splitlines(keepends=True) 按行分割,保留空格,生成列表
d = difflib.Differ()
print(''.join(d.compare(text1,text2)))
执行结果:
- 1. Beautiful is better than ugly.+ 1. Beautiful is better than ugly.
? +
- 2. Explicit is better than implicit.- 3. Simple is better than complex.? -
+ 3.Simple is better than complex.
? +
- 4. Complex