l = map(chr, xrange(256)) #將ascii轉爲字符串 _idmap = str('').join(l) del l # Construct a translation string _idmapL = None #定義一個全局變量 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) #將ascii 字符串轉換爲列表 L = _idmapL[:] #列表對象淺拷貝,目的是爲了避免影響global , or [x for x in _idmapL] or L = list(_idmapL) or L = copy.copy(_idmapL) fromstr = map(ord, fromstr) #功能與chr相反,即將char轉換爲對應的數字編碼 for i in range(len(fromstr)): L[fromstr[i]] = tostr[i] #替換 return ''.join(L) #重組