Differences between Maketrans and translate functions under python2.x and 3.x _python

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 differences in operating under 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 divides the string into two basic types: The Unicode string and the 8-bit string str, which contains the byte data and our common ASCII code data; 3. The X version again divides the string into byte string bytes and literal string str, both immutable, so a mutable byte string type ByteArray is added.
In the 2.X version, a large number of string types and str, Unicode types are duplicated, so 3. The X version does not advocate using a method that is duplicated with STR in the string module. There are also a number of useful constants and methods in the string module, such as String.digits, that can be easily used in string encoding.

Signature of the 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 the 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)

It can be seen that, relative to 2. X's Maketrans method of the string module, 3.X provides three static methods for creating the mapping table respectively.
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 contains ' 1 ', ' 2 ', ' 3 ' replaced by ' a ', ' B ', ' C '
  >>> s = ' 54321123789 '                #转换前的字符串
  >>> s.translate (map)                  # Convert the string
  ' 54cbaabc789 ' #转换后的字符串 with the created mapping table map                        

The demo process under 3.X:

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

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

  >>> Import string
  >>> map = String.maketrans (' 123 ', ' abc ')
  >>> s = ' 54321123789 '
  >>> s.translate (map, ' #除了转换 ')        , and delete the characters in the string ' 7 ', ' 8 '
  54cbaabc9 '               #转换后的字符串没有字符 ' 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 encountered a 2 based. The example of the X version is as follows

  Import String
  def translator (frm= ', to= ', delete= ', keep=none):
    If Len (to) = = 1: to
      = to * len (frm)
    tra NS = String.maketrans (frm, to)
    if keep isn't 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 returned mapping table, why can also call the Translate method, so it should be a str type, the test is as follows:

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

In the 3. In the X version, this method does not work correctly, so where is the error? Let's look at what the mapping table is:

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

Knowing the type of the mapping table, we can "post-process" it, like the example in Python cookbook above, to meet our coding requirements.

The examples discussed above are composed of ASCII characters, and if it is a byte type, the operation in the 2.X version is the same, and the function of bytes or ByteArray is called in 3.X; if Unicode type, 2.X needs 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 results are not the same, the lookup manual shows that the mapping table of the Translate method of Unicode is that the key of the dictionary must be the bit ordinal of Unicode, and the value can be the bit ordinal of Unicode, the 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.