本文整理匯總了Python中strop.maketrans方法的典型用法代碼示例。如果您正苦於以下問題:Python strop.maketrans方法的具體用法?Python strop.maketrans怎麽用?Python strop.maketrans使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類strop
的用法示例。
在下文中一共展示了strop.maketrans方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: maketrans
# 需要導入模塊: import strop [as 別名]
# 或者: from strop import maketrans [as 別名]
def maketrans(fromstr, tostr):
"""maketrans(frm, to) -> string
Return a translation table (a string of 256 bytes long)
suitable for use in string.translate. The strings frm and to
must be of the same length.
"""
if len(fromstr) != len(tostr):
raise ValueError, "maketrans arguments must have same length"
global _idmapL
if not _idmapL:
_idmapL = map(None, _idmap)
L = _idmapL[:]
fromstr = map(ord, fromstr)
for i in range(len(fromstr)):
L[fromstr[i]] = tostr[i]
return ''.join(L)
####################################################################
示例2: maketrans
# 需要導入模塊: import strop [as 別名]
# 或者: from strop import maketrans [as 別名]
def maketrans(fromstr, tostr):
"""maketrans(frm, to) -> string
Return a translation table (a string of 256 bytes long)
suitable for use in string.translate. The strings frm and to
must be of the same length.
"""
if len(fromstr) != len(tostr):
raise ValueError, "maketrans arguments must have same length"
global _idmapL
if not _idmapL:
_idmapL = list(_idmap)
L = _idmapL[:]
fromstr = map(ord, fromstr)
for i in range(len(fromstr)):
L[fromstr[i]] = tostr[i]
return ''.join(L)
####################################################################
示例3: maketrans
# 需要導入模塊: import strop [as 別名]
# 或者: from strop import maketrans [as 別名]
def maketrans(fromstr, tostr):
"""maketrans(frm, to) -> string
Return a translation table (a string of 256 bytes long)
suitable for use in string.translate. The strings frm and to
must be of the same length.
"""
if len(fromstr) != len(tostr):
raise ValueError, "maketrans arguments must have same length"
global _idmapL
if not _idmapL:
_idmapL = list(_idmap)
L = _idmapL[:]
fromstr = map(ord, fromstr)
for i in range(len(fromstr)):
L[fromstr[i]] = tostr[i]
return join(L, "")
# Substring replacement (global)
示例4: test_maketrans
# 需要導入模塊: import strop [as 別名]
# 或者: from strop import maketrans [as 別名]
def test_maketrans(self):
self.assertTrue(strop.maketrans("abc", "xyz") == transtable)
self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")