Chinaunix首页 | 论坛 | 博客
  • 博客访问: 94763
  • 博文数量: 16
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-04 15:33
文章分类

全部博文(16)

文章存档

2009年(5)

2008年(11)

我的朋友

分类: Python/Ruby

2008-05-15 16:45:44

python中字符串函数的使用
 
1.string.maketrans与string.translate
 
 由于translate方法需要一个映射表,然后根据这个映射表将字符串里的字符转变成映射表里的字符。例如:设定字符串里所有的a都映射成1,b映射成2,c映射成3。当我们调用'abc'.translate的方法时,它就会返回'123'。  
maketrans is a utility function to create translation tables. (A translation table is a string t of exactly 256 characters: when you pass t as the first argument of a translate method, each character c of the string on which you call the method is translated in the resulting string into the character t[ord(c)].)
    
  那么这个映射表是怎么来的呢?这里就是string.maketrans的任务了。你给它两个等长的字符串,它就根据位置,将这两个字符串里的字符做成映射表。字符串的长度不能超过256,后面的定义override前面的定义。
translate( table[, deletechars])
Return a copy of the string 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.

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


 

阅读(1934) | 评论(0) | 转发(0) |
0

上一篇:python正则表达式

下一篇:转载 Junit测试

给主人留下些什么吧!~~