Python——string之maketrans,translate函數

來源:互聯網
上載者:User

        先來看下關於這兩個函數的官方定義:

        string.maketrans(from, to):Return a translation table suitable for passing to translate(), that will map each character in from into the character at the same position in to; from and to must have the same length.

        string.translate(s, table[, deletechars]):Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for
each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

        下面的代碼是對這兩個函數進行的封裝:

#!/usr/bin/env python# -*- coding:utf-8 -*-import stringdef translator(frm='', to='', delete='', keep=None):    if len(to) == 1:        to = to * len(frm)        trans = string.maketrans(frm, to)    if keep is not None:        trans_all = string.maketrans('', '')        #keep.translate(trans_all, delete),從要保留的字元中剔除要刪除的字元        #trans_all.translate(trans_all, keep.translate(trans_all, delete)),從翻譯表中刪除要保留的字元,即取保留字元的補集        delete = trans_all.translate(trans_all, keep.translate(trans_all, delete))            def translate(s):        return s.translate(trans, delete)            return translateif __name__ == '__main__':    #result:12345678    digits_only = translator(keep=string.digits)    print digits_only('Eric chen: 1234-5678')        #result:Eric chen: -    no_digits = translator(delete=string.digits)    print no_digits('Eric chen: 1234-5678')        #result:Eric chen: ****-****    digits_to_hash = translator(frm=string.digits, to='*')    print digits_to_hash('Eric chen: 1234-5678')

        當以string.maketrans('', '')方法調用maketrans時,翻譯表正好是有256個字元的字串t。翻譯表產生的字串(忽略不可列印字元)為“!"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~”,本質上與ASCII表相對應。

        其實調用maketrans函數時,已經完成了轉換。例如string.maketrans('ABCD', 'abcd'),調用完成後,翻譯表產生的包含256個字元的字串(忽略不可列印字元)為“!"#$%&'()*+,-./0123456789:;<=>?@abcdEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~”,該翻譯表中的原“ABCD”的位置已被“abcd”替換。

        當你把t作為第一個參數傳入translate方法時,原字串中的每一個字元c,在處理完成後都會被翻譯成字元t[ord(c)]。


        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.

        下面的代碼是對unicode字串進行過濾:

import setsclass 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 = Keeperif __name__ == '__main__':    #result:人民    just_people = makeFilter(u'人民')    print just_people(u'中華人民共和國成立了')    #刪除unicode字元    #result:中華共和國成立了!    translate_table = dict((ord(char), None) for char in u'人民')    print unicode(u'中華人民共和國成立了!').translate(translate_table)        #替換unicode字元    #result:中華**共和國成立了!    translate_table = dict((ord(char), u'*') for char in u'人民')    print unicode(u'中華人民共和國成立了!').translate(translate_table)

        Unicode字串的translate方法只需要一個參數:一個序列或映射,並且根據字串中的每個字元的碼值進行索引。碼值不是一個映射的鍵(或者序列的索引值)的字元會被直接複製,不做改變。與每個字元碼對應的值必須是一個unicode字串(該字元的替換物)或者None(這意味著該字元需要被刪除)。通常我們使用dict或list作為unicode字串的translate方法的參數,來翻譯或刪除某些字元。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.