python2.x and 3.x Maketrans differ from the use of translate functions

Source: Internet
Author: User
The Maketrans and translate functions are common methods for string character encoding. This article focuses on demonstrating its basic usage and the difference between operating in different versions. This article refers to the 2. The x version refers to more than 2.6 versions, and the 3.X version refers to more than 3.1 versions.
The 2.X version essentially divides the string into two types: A Unicode string and a 8-bit string, str, which contains byte data and our common ASCII code data, while 3. The x version re-divides the string into byte string bytes and the text string str, both immutable, so a variable byte string type ByteArray is added.
A large number of methods for string types and str, Unicode types in the 2.X version are duplicated, so 3. The X version does not advocate the use of the method repeated with STR in the string module. There are also many useful constants and methods in the string module, such as String.digits, which can be easily used in string encoding.

Signature of Maketrans and translate functions in 2.X:

  String.maketrans (from, to)  String.translate (S, table[, Deletechars])  str.translate (table[, Deletechars])  unicode.translate (table)

Signature of Maketrans and translate functions in 3.X:

  Static Str.maketrans (x[, y[, z])  static Bytes.maketrans (from, to)  static Bytearray.maketrans (from, to)  Str.translate (map)  bytes.translate (table[, delete])  bytearray.translate (table[, delete])

As can be seen, relative to 2. The Maketrans method of the X string module, which provides three static methods for creating a mapping table, is provided in 3.X.
Let's look at a simple example to illustrate the process of string conversion:
The demo process under 2.X:

  >>> Import string                     #导入string模块  >>> map = String.maketrans (' 123 ', ' abc ') #建立映射表, the string containing ' 1 ', ' 2 ', ' 3 ' replaced by ' a ', ' B ', ' C '  >>> s = ' 54321123789 '                #转换前的字符串  >>> s.translate (map)                  # Convert string  ' 54cbaabc789 ' #转换后的字符串 with map created                        

The demo process under 3.X:

  >>> map = Str.maketrans (' 123 ', ' abc ')  >>> s = ' 54321123789 '  >>> s.translate (map) c18/> ' 54cbaabc789 '

2.X uses the Maketrans function of string, and 3. X uses the Maketrans function of STR, except this, the use method is basically the same. If you specify a character in a string to be deleted, the use will be slightly different, as follows:
The demo process under 2.X:

  >>> Import string  >>> map = String.maketrans (' 123 ', ' abc ')  >>> s = ' 54321123789 '  >>> s.translate (map, ' 54cbaabc9 ')        #除了转换, also remove the characters in the string ' 7 ', ' 8 '               #转换后的字符串没有字符 ' 7 ', ' 8 '

The demo process under 3.X:

  >>> map = Str.maketrans (' 123 ', ' abc ', ' #要删除的字符需要在这指定 ')  >>> s = ' 54321123789 '  >>> S.translate (map)  ' 54cbaabc9 '

I was reading "Python Cookbook" and I met a 2-based. An example of the X version is as follows

  Import String  def translator (frm= ", to=", delete= ", Keep=none):    If len = = 1: to      = to * len (frm)    tra NS = String.maketrans (frm, to)    if keep are not None:      allchars = String.maketrans (",")      delete = Allchars.tran Slate (Allchars, Keep.translate (Allchars,delete))    def translate (s):      return s.translate (trans, delete)    return translate

Allchars should be a return mapping table, why you can also call the Translate method, so it should be a str type, tested as follows:

  >>> Import string  >>> map = String.maketrans (' 123 ', ' abc ')  >>> type (map)  
 
  

In the 3. In the X version, this method does not run properly, so where is the wrong place, let's see what the mapping table is type:

  >>> map = Str.maketrans (' 123 ', ' abc ')  >>> type (map)  
 
  

Knowing the type of the mapping table, we can "post-processing" it, as in the above "Python Cookbook" example, to meet our coding requirements.

The examples discussed above use a string of ASCII characters, if it is byte type, 2.X version of the operation is the same, 3.X call bytes or ByteArray function, if the Unicode type, 2.X need to use the Unicode translate method, Note the following code

  >>> print U "Hallo". Translate ({97:u ' e '})  Hello  >>> print U "Hallo". Translate ({' A ': U ' e '})  Hallo  >>> print U "Hallo". Translate ({u ' a ': U ' e '})  Hallo

The result is different, and the lookup manual shows that the mapping table of the Unicode translate method, the dictionary key must be a Unicode bit ordinal, the value can be a Unicode bit ordinal, a Unicode string, or none.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.