Python translator使用實(shí)例
更新時(shí)間:2008年09月06日 13:44:27 作者:
translator實(shí)例應(yīng)用代碼
1.string.maketrans設(shè)置字符串轉(zhuǎn)換規(guī)則表(translation table)
allchars = string.maketrans('', '')#所有的字符串,即不替換字符串
aTob = string.maketrans('a','b')#將字符a轉(zhuǎn)換為字符b
2.translate函數(shù)進(jìn)行字符串的替換和刪除,第一個(gè)參數(shù)是字符串轉(zhuǎn)換規(guī)則表(translation table),第二個(gè)參數(shù)是要?jiǎng)h除的字符串。比如,要將字符串s中的所有e替換為a,同時(shí)要?jiǎng)h除所有的o
aTob = string.maketrans('e','a')
s = 'hello python'
print s.translate(aTob, 'o')
輸出結(jié)果:
hall pythn
3.假如我們這樣使用
allchars = string.maketrans('', '')
k = allchars.translate(allchars, 'a')
allchars表示所有的字符串,而k表示從所有的字符串中去除掉字符a,就是說所有的字符,除了a,因此,我們再調(diào)用如下方法時(shí):
s = 'abc'
print s.translate(allchars, k)
字面意思是,輸出“字符串s中除去任何不是字符a的字符",即,只輸出字符a,因此輸出結(jié)果為:
a
4.現(xiàn)在,已經(jīng)不難理解下面這個(gè)函數(shù)了
import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate調(diào)用:
digits_only = translator(keep=string.digits)
print digits_only('Chris Perkins : 224-7992')
digits_to_hash = translator(frm=string.digits, to='#')
print digits_to_hash('Chris Perkins : 224-7992')
輸出結(jié)果:
2247992
Chris Perkins : ###-####
復(fù)制代碼 代碼如下:
allchars = string.maketrans('', '')#所有的字符串,即不替換字符串
aTob = string.maketrans('a','b')#將字符a轉(zhuǎn)換為字符b
2.translate函數(shù)進(jìn)行字符串的替換和刪除,第一個(gè)參數(shù)是字符串轉(zhuǎn)換規(guī)則表(translation table),第二個(gè)參數(shù)是要?jiǎng)h除的字符串。比如,要將字符串s中的所有e替換為a,同時(shí)要?jiǎng)h除所有的o
復(fù)制代碼 代碼如下:
aTob = string.maketrans('e','a')
s = 'hello python'
print s.translate(aTob, 'o')
輸出結(jié)果:
hall pythn
3.假如我們這樣使用
復(fù)制代碼 代碼如下:
allchars = string.maketrans('', '')
k = allchars.translate(allchars, 'a')
allchars表示所有的字符串,而k表示從所有的字符串中去除掉字符a,就是說所有的字符,除了a,因此,我們再調(diào)用如下方法時(shí):
復(fù)制代碼 代碼如下:
s = 'abc'
print s.translate(allchars, k)
字面意思是,輸出“字符串s中除去任何不是字符a的字符",即,只輸出字符a,因此輸出結(jié)果為:
a
4.現(xiàn)在,已經(jīng)不難理解下面這個(gè)函數(shù)了
復(fù)制代碼 代碼如下:
import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate調(diào)用:
復(fù)制代碼 代碼如下:
digits_only = translator(keep=string.digits)
print digits_only('Chris Perkins : 224-7992')
digits_to_hash = translator(frm=string.digits, to='#')
print digits_to_hash('Chris Perkins : 224-7992')
輸出結(jié)果:
2247992
Chris Perkins : ###-####
相關(guān)文章
Python調(diào)用C語言開發(fā)的共享庫方法實(shí)例
這篇文章主要介紹了Python調(diào)用C語言開發(fā)的共享庫方法實(shí)例,本文同時(shí)給出了C語言和Python調(diào)用簡單實(shí)例,需要的朋友可以參考下2015-03-03Python?pycryptodome庫實(shí)現(xiàn)RSA加密解密消息
本文為大家介紹了如何在?Python?中使用?RSA?公鑰加密技術(shù)來加密和解密消息,并使用?pycryptodome?庫進(jìn)行實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助2024-02-02Python采集王者最低戰(zhàn)力信息實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Python采集王者最低戰(zhàn)力信息實(shí)戰(zhàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04python控制nao機(jī)器人身體動作實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了python控制nao機(jī)器人身體動作實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Python利用正則表達(dá)式匹配并截取指定子串及去重的方法
這篇文章主要介紹了Python利用正則表達(dá)式匹配并截取指定子串及去重的方法,涉及Python正則表達(dá)式匹配及字符串截取操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07