99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
2018-10-26 閱讀量: 938
如何用Python來進(jìn)行查詢和替換一個文本字符串?

可以使用sub()方法來進(jìn)行查詢和替換,sub方法的格式為:sub(replacement, string[, count=0])
replacement是被替換成的文本
string是需要被替換的文本
count是一個可選參數(shù),指最大被替換的數(shù)量

例子:

import re
p = re.compile('(blue|white|red)')
print(p.sub('colour','blue socks and red shoes'))
print(p.sub('colour','blue socks and red shoes', count=1))

輸出:

colour socks and colour shoes
colour socks and red shoes

subn()方法執(zhí)行的效果跟sub()一樣,不過它會返回一個二維數(shù)組,包括替換后的新的字符串和總共替換的數(shù)量

例如:

import re
p = re.compile('(blue|white|red)')
print(p.subn('colour','blue socks and red shoes'))
print(p.subn('colour','blue socks and red shoes', count=1))

輸出

('colour socks and colour shoes', 2)
('colour socks and red shoes', 1)

17.3451
1
關(guān)注作者
收藏
評論(0)

發(fā)表評論

暫無數(shù)據(jù)
推薦帖子