本文整理汇总了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
示例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)
示例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
示例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()])
示例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
#
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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