分类: Python/Ruby
2008-05-15 16:45:44
table[, deletechars]) |
You can use the maketrans() helper function in the module to create a translation table.
For Unicode objects, the translate() method does not accept the optional deletechars argument. Instead, it returns a copy of the s where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None
. Unmapped characters are left untouched. Characters mapped to None
are deleted. Note, a more flexible approach is to create a custom character mapping codec using the module (see encodings.cp1251 for an example).
例如:
import string
#string.maketrans(frm,to)中,len(frm)==len(to)否则,会抛出ValueError异常
trans = string.maketrans('a','*')
str = 'abc'
straAft = str.translate(trans,'b')#'b'是deleteChar
则有:straAft = '*c'
下面再举一个Python-CookBook上的例子:
import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)#防止出现上面提到的ValueError异常
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))#allChars - keep
def translate(s):
return s.translate(trans, delete)
return translate
if __name__ == '__main__':
digits_only = translator(keep=string.digits)
digits_only('Chris Perkins : 224-7992')
A Unicode string's translate method takes only one argument: a mapping or sequence, which is indexed with the code number of each character in the string. Characters whose codes are not keys in the mapping (or indices in the sequence) are just copied over to the output string. Otherwise, the value corresponding to each character's code must be either a Unicode string (which is substituted for the character) or None (in which case the character is deleted). A very nice and powerful arrangement, but unfortunately not one that's identical to the way plain strings work, so we must recode.
Normally, we use either a dict or a list as the argument to a Unicode string's translate method to translate some characters and/or delete some. But for the specific task of this recipe (i.e., keep just some characters, delete all others), we might need an inordinately large dict or string, just mapping all other characters to None. It's better to code, instead, a little class that appropriately implements a _ _getitem_ _ method (the special method that gets called in indexing operations). Once we're going to the (slight) trouble of coding a little class, we might as well make its instances callable and have makefilter be just a synonym for the class itself:
import sets class Keeper(object): def _ _init_ _(self, keep): self.keep = sets.Set(map(ord, keep)) def _ _getitem_ _(self, n): if n not in self.keep: return None return unichr(n) def _ _call_ _(self, s): return unicode(s).translate(self) makefilter = Keeper if _ _name_ _ == '_ _main_ _': just_vowels = makefilter('aeiouy') print just_vowels(u'four score and seven years ago') # emits: ouoeaeeyeaao print just_vowels(u'tiger, tiger burning bright') # emits: ieieuii