先贴一段代码(from python cookbook)
这两个函数说明如下:
这里指定的intab,outtab是一一对应的关系,如string.maketrans('ab','cd')则表示a变为c,b变为d
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.
string.translate可以接受第一个参数作为table,第二个参数(可选)指定要删除的字符
import string
s = 'abcdefg-1234567'
table = string.maketrans('', '') #没有映射,实际上就是按原始字符保留,看下面用到translate中时的效果
s.translate(table) # 输出abcdefg-1234567
s.translate(table, 'abc123') #输出defg-4567 可以看到删除了字符abc123
#下面再看有字符映射时的效果
table = string.maketrans('abc', 'ABC') #用于translate中时的效果如下
s.translate(table) #输出ABCdefg-1234567 就是将abc映射为大写的ABC,前提是abc如果被保留下来了
s.translate(table, 'ab123') #输出Cdefg-4567 先把s中的ab123去除了,然后在保留下来的字符中应用table中指定的字符映射关系映射:c -> C
type(table)得到结果是<type 'str'>
L1表示所有字符串,allchars类型是str所以有translate方法
L2表示在所有字符串(allchars)里删除'abc',剩下的字符赋给delete
这个有什么作用呢?
我们再应用一句
keep='abcdefg'.translate(allchars,delete)可以得到你需要保留的字符串
现在刚开始的程序结果应该就不难理解了
2247992
Chris Perkins : -
Chris Perkins : ###-####
ef
ps:
程序是以delete为主的,即如果字符同时在keep和delete里,那么就delete掉。
如果不想这样的话
delete=allchars.translate(allchars,keep.translate(allchars,delete))
这句改成
delete=allchars.translate(allchars,keep)即可
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( '' , '' )
# print allchars
delete = allchars.translate(allchars,keep.translate(allchars,delete))
# print delete
def translate(s):
# print "delete=",delete
return s.translate(trans,delete)
return translate
digits_only = translator(keep = string.digits)
print digits_only( ' Chris Perkins : 224-7992 ' )
no_digits = translator(delete = string.digits)
print no_digits( ' Chris Perkins : 224-7992 ' )
digits_to_hash = translator(frm = string.digits,to = ' # ' )
print digits_to_hash( ' Chris Perkins : 224-7992 ' )
trans = translator(delete = ' abcd ' ,keep = ' cdef ' )
print trans( ' abcdefg ' )
这个函数是一个包装后的函数,基于string.maketrans(),string.translate()
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( '' , '' )
# print allchars
delete = allchars.translate(allchars,keep.translate(allchars,delete))
# print delete
def translate(s):
# print "delete=",delete
return s.translate(trans,delete)
return translate
digits_only = translator(keep = string.digits)
print digits_only( ' Chris Perkins : 224-7992 ' )
no_digits = translator(delete = string.digits)
print no_digits( ' Chris Perkins : 224-7992 ' )
digits_to_hash = translator(frm = string.digits,to = ' # ' )
print digits_to_hash( ' Chris Perkins : 224-7992 ' )
trans = translator(delete = ' abcd ' ,keep = ' cdef ' )
print trans( ' abcdefg ' )
这两个函数说明如下:
string.maketrans返回一个table用于string.translate函数string.maketrans(intab, outtab) --> This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that both intab and outtab must have the same length.
这里指定的intab,outtab是一一对应的关系,如string.maketrans('ab','cd')则表示a变为c,b变为d
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.
string.translate可以接受第一个参数作为table,第二个参数(可选)指定要删除的字符
import string
s = 'abcdefg-1234567'
table = string.maketrans('', '') #没有映射,实际上就是按原始字符保留,看下面用到translate中时的效果
s.translate(table) # 输出abcdefg-1234567
s.translate(table, 'abc123') #输出defg-4567 可以看到删除了字符abc123
#下面再看有字符映射时的效果
table = string.maketrans('abc', 'ABC') #用于translate中时的效果如下
s.translate(table) #输出ABCdefg-1234567 就是将abc映射为大写的ABC,前提是abc如果被保留下来了
s.translate(table, 'ab123') #输出Cdefg-4567 先把s中的ab123去除了,然后在保留下来的字符中应用table中指定的字符映射关系映射:c -> C
type(table)得到结果是<type 'str'>
allchars
=
string.maketrans(
''
,
''
)
delete = allchars.translate(allchars, ' abc ' )
解释下:
delete = allchars.translate(allchars, ' abc ' )
L1表示所有字符串,allchars类型是str所以有translate方法
L2表示在所有字符串(allchars)里删除'abc',剩下的字符赋给delete
这个有什么作用呢?
我们再应用一句
keep='abcdefg'.translate(allchars,delete)可以得到你需要保留的字符串
现在刚开始的程序结果应该就不难理解了
2247992
Chris Perkins : -
Chris Perkins : ###-####
ef
ps:
程序是以delete为主的,即如果字符同时在keep和delete里,那么就delete掉。
如果不想这样的话
delete=allchars.translate(allchars,keep.translate(allchars,delete))
这句改成
delete=allchars.translate(allchars,keep)即可