Let's take a look at the official definitions of these two functions:
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; 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
Each character value, indexed by its ordinal. If table is none, then only the character deletion step is already med.
The following code encapsulates these two functions:
#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import stringdef Translator (FRM = '', To ='', delete = '', keep = none): If Len (to) = 1: To = * Len (FRM) Trans = string. maketrans (FRM, To) If keep is not none: trans_all = string. maketrans ('','') # Keep. translate (trans_all, delete), remove the characters to be deleted from the characters to be retained # trans_all.translate (trans_all, keep. translate (trans_all, delete), and delete the characters to be retained from the translation table, that is, take the supplement set of the reserved characters 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 ')
When you call maketrans using the string. maketrans ('','') method, the translation table exactly contains the string T with 256 characters. The string generated by the translation table (ignore non-printable characters) is "! "# $ % & '() * +,-./:; <=>? @ Abcdefghijklmnopqrstuvwxyz [\] ^ _ 'abcdefghijklmnopqrstuvwxyz {| }~", Essentially, it corresponds to an ASCII table.
In fact, the conversion has been completed when the maketrans function is called. For example, String. maketrans ('abcd', 'abcd'), after the call is completed, the string generated by the translation table containing 256 characters (ignore non-printable characters) is "! "# $ % & '() * +,-./0123456789:; <=>? @ Abcdefghijklmnopqrstuvwxyz [\] ^ _ 'abcdefghijklmnopqrstuvwxyz {| }~", The original "ABCD" position in the translation table has been replaced by "ABCD.
When you pass T as the first parameter to the translate method, each character C in the original string will be translated into T [ord (c)] after processing.
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.
The following code filters Unicode strings:
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: People's just_people = makefilter (U 'others ') print just_people (u'people's Republic of China was founded ') # Delete Unicode characters # result: the People's Republic of China was founded! Translate_table = dict (ord (char), none) for char in u'people') print Unicode (U' People's Republic of China was founded! '). Translate (translate_table) # Replace Unicode characters # result: the Republic of China was founded! Translate_table = dict (ord (char), U' * ') for char in u'peoples') print Unicode (u'people's Republic of China was founded! '). Translate (translate_table)
The translate method of a unicode string requires only one parameter: a sequence or ing, and is indexed Based on the code value of each character in the string. The characters of the key (or index value of the sequence) that are not mapped will be directly copied and not changed. The value corresponding to each escape code must be a unicode string (Replacement of the character) or none (which means the character needs to be deleted ). We usually use dict or list as the parameter of the translate method of Unicode string to translate or delete some characters.