當前位置: 首頁>>代碼示例>>Python>>正文


Python string.translate方法代碼示例

本文整理匯總了Python中string.translate方法的典型用法代碼示例。如果您正苦於以下問題:Python string.translate方法的具體用法?Python string.translate怎麽用?Python string.translate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在string的用法示例。


在下文中一共展示了string.translate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: derot

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def derot(xset, hset, src):
    xset = eval(xset)
    hset = eval(hset)

    import string
    o = ''
    u = ''
    il = 0
    for first in hset:
        u += first
        o += xset[il]
        il += 1
    rot13 = string.maketrans(o, u)
    link = string.translate(src, rot13)
    xbmc.log('@#@DEROT-LINK: %s' % link, xbmc.LOGNOTICE)
    return link 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:18,代碼來源:sport365.py

示例2: dump_dirs

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def dump_dirs (self, msg):
        if DEBUG:
            from distutils.fancy_getopt import longopt_xlate
            print msg + ":"
            for opt in self.user_options:
                opt_name = opt[0]
                if opt_name[-1] == "=":
                    opt_name = opt_name[0:-1]
                if opt_name in self.negative_opt:
                    opt_name = string.translate(self.negative_opt[opt_name],
                                                longopt_xlate)
                    val = not getattr(self, opt_name)
                else:
                    opt_name = string.translate(opt_name, longopt_xlate)
                    val = getattr(self, opt_name)
                print "  %s: %s" % (opt_name, val) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:18,代碼來源:install.py

示例3: do_simple_substitution

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def do_simple_substitution(ciphertext, pt_charset, ct_charset):
   '''
   Perform simple substitution based on character sets

   Simplifies the use of string.translate(). If, for instance, you wish to
   transform a ciphertext where 'e' is swapped with 't', you would call this
   function like so:

   do_simple_substitution('Simplt subeieueion ciphtrs art silly','et','te')
   
   ciphertext - A string to translate
   pt_charset - The character set of the plaintext, usually 'abcdefghijk...xyz'
   ct_charset - The character set of the ciphertext
   '''
   #translate ciphertext to plaintext using mapping
   return string.translate(ciphertext, string.maketrans(ct_charset, pt_charset))


# TODO: Implement chi square 
開發者ID:nccgroup,項目名稱:featherduster,代碼行數:21,代碼來源:helpers.py

示例4: morse_decode

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def morse_decode(text, dot='.', dash='-', space=' '):
   '''
   Decodes a Morse encoded message. Optionally, you can provide an alternate
   single character for dot, dash, and space.

   Parameters:
   text - (string) A message to decode
   dot - (char) An alternate dot char
   dash - (char) An alternate dash char
   space - (char) A char to split the text on
   '''
   inverse_morse_table = map(lambda (x,y): (y,x), morse_table.items())
   dot_dash_trans = string.maketrans('.-', dot+dash)
   inverse_morse_table = [(string.translate(x,dot_dash_trans), y) for (x,y) in inverse_morse_table]
   inverse_morse_table = dict(inverse_morse_table)
   return ''.join([inverse_morse_table[char] for char in text.split(space) if char in inverse_morse_table.keys()]) 
開發者ID:nccgroup,項目名稱:featherduster,代碼行數:18,代碼來源:classical.py

示例5: _normalize

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def _normalize(title, charset='utf-8'):
    '''Removes all accents and illegal chars for titles from the String'''
    if isinstance(title, unicode):
        title = string.translate(title, allchars, deletechars)
        try:
            title = title.encode("utf-8")
            title = normalize('NFKD', title).encode('ASCII', 'ignore')
        except UnicodeEncodeError:
            logger.error("Error de encoding")
    else:
        title = string.translate(title, allchars, deletechars)
        try:
            # iso-8859-1
            title = title.decode(charset).encode('utf-8')
            title = normalize('NFKD', unicode(title, 'utf-8'))
            title = title.encode('ASCII', 'ignore')
        except UnicodeEncodeError:
            logger.error("Error de encoding")
    return title

    # 
開發者ID:alfa-addon,項目名稱:addon,代碼行數:23,代碼來源:subtitletools.py

示例6: b64sub

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def b64sub(s, key):
    """
    "Encryption" method that base64 encodes a given string, 
    then does a randomized alphabetic letter substitution.
    """
    enc_tbl = string.maketrans(string.ascii_letters, key)
    return string.translate(base64.b64encode(s), enc_tbl) 
開發者ID:HarmJ0y,項目名稱:Arya,代碼行數:9,代碼來源:arya.py

示例7: update

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def update(self, data):
        if self._op:
            return translate(data, self._encrypt_table)
        else:
            return translate(data, self._decrypt_table) 
開發者ID:ntfreedom,項目名稱:neverendshadowsocks,代碼行數:7,代碼來源:table.py

示例8: test_bad_str_translate_call_string_literal

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_bad_str_translate_call_string_literal(self):
        node = astroid.extract_node(
            """
         foobar.translate(None, 'abc123') #@
         """
        )
        message = testutils.Message(
            "deprecated-str-translate-call", node=node, confidence=INFERENCE_FAILURE
        )
        with self.assertAddsMessages(message):
            self.checker.visit_call(node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:13,代碼來源:unittest_checker_python3.py

示例9: test_bad_str_translate_call_variable

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_bad_str_translate_call_variable(self):
        node = astroid.extract_node(
            """
         def raz(foobar):
           foobar.translate(None, 'hello') #@
         """
        )
        message = testutils.Message(
            "deprecated-str-translate-call", node=node, confidence=INFERENCE_FAILURE
        )
        with self.assertAddsMessages(message):
            self.checker.visit_call(node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:14,代碼來源:unittest_checker_python3.py

示例10: test_bad_str_translate_call_infer_str

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_bad_str_translate_call_infer_str(self):
        node = astroid.extract_node(
            """
         foobar = "hello world"
         foobar.translate(None, foobar) #@
         """
        )
        message = testutils.Message(
            "deprecated-str-translate-call", node=node, confidence=INFERENCE
        )
        with self.assertAddsMessages(message):
            self.checker.visit_call(node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:14,代碼來源:unittest_checker_python3.py

示例11: test_ok_str_translate_call_integer

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_ok_str_translate_call_integer(self):
        node = astroid.extract_node(
            """
         foobar.translate(None, 33) #@
         """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:10,代碼來源:unittest_checker_python3.py

示例12: test_ok_str_translate_call_keyword

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_ok_str_translate_call_keyword(self):
        node = astroid.extract_node(
            """
         foobar.translate(None, 'foobar', raz=33) #@
         """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:10,代碼來源:unittest_checker_python3.py

示例13: test_six_ifexp_conditional

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_six_ifexp_conditional(self):
        code = """
        from __future__ import absolute_import
        import six
        import string
        string.translate if six.PY2 else None
        """
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:12,代碼來源:unittest_checker_python3.py

示例14: test_ok_str_translate_call_not_str

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def test_ok_str_translate_call_not_str(self):
        node = astroid.extract_node(
            """
         foobar = {}
         foobar.translate(None, 'foobar') #@
         """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:11,代碼來源:unittest_checker_python3.py

示例15: _quote

# 需要導入模塊: import string [as 別名]
# 或者: from string import translate [as 別名]
def _quote(str, LegalChars=_LegalChars,
           idmap=_idmap, translate=string.translate):
    #
    # If the string does not need to be double-quoted,
    # then just return the string.  Otherwise, surround
    # the string in doublequotes and precede quote (with a \)
    # special characters.
    #
    if "" == translate(str, idmap, LegalChars):
        return str
    else:
        return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
# end _quote 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:15,代碼來源:Cookie.py


注:本文中的string.translate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。