Python中的內建difflib模組

2021-09-27 11:20:16 字數 1665 閱讀 5460

什麼是difflib? 用來做什麼?

difflib為python的標準庫模組,無需安裝。

作用是對比文字之間的差異。並且支援輸出可讀性比較強的html文件,與linux下的diff 命令相似。

在版本控制方面非常有用。

符號理解

符號 含義

『-』 包含在第乙個系列行中,但不包含第二個。

『+』 包含在第二個系列行中,但不包含第乙個。

』 』 兩個系列行一致

『?』 存在增量差異

『^』 存在差異字元

text1 = '''  1. beautiful is better than ugly.

2. explicit is better than implicit.

3. ****** is better than complex.

4. complex is better than complicated.

'''text2 = ''' 1. beautiful is better than ugly.

3. ****** is better than complex.

4. complicated is better than complex.

5. flat is better than nested.

'''

import difflib

text1 = ''' 1. beautiful is better than ugly.

2.explicit is better than implicit.

****** is better than complex.

3.complex is better than complicated. '''.splitlines(keepends=true)

text2 = ''' 1. beautiful is better than ugly.

3.****** is better than complex.

4.complicated is better than complex.

5.t is better than nested.

'''.splitlines(keepends=true)

"""splitlines()按照行分割

返回乙個包含各行作為元素的列表

引數:keepends=true 保留換行符

keepends=false 不包含換行符

"""# 實現類似linux裡面的diff命令的功能

python 中的內建函式

built in functions abs divmod input open staticmethod all enumerate int ord str any eval isinstance pow sum basestring execfile issubclass print super...

python中的內建函式

以下是目前我們已經滲透過的內建函式 int 建立或者將其他資料轉化為整型float 建立或者將其他資料轉化為浮點型bool 建立或者將其他資料轉化為布林型complex 建立或者將其他資料轉化為複數str 建立或者將其他資料轉化為字串list 建立或者將其他資料轉化為列表tuple 建立或者將其他資...

Python中的內建函式

len s 返回物件內元素的個數。dic s lst 2 3,4 5,6 6 print len dic print len s print len lst 輸出結果 2 46max iterable,key,default 返回最大值。max 1 max 3,1,2,6 max 2 max def...