This article mainly introduces Python2.x and 3. in x, maketrans and translate functions are different in usage. the ing between the two functions is the basic knowledge in Python learning, for more information, see the maketrans and translate functions. This article focuses on the differences between its basic usage and operations in different versions. The 2.x version mentioned in this article refers to the version above 2.6, and the 3.x version refers to the version above 3.1.
2. in version X, there are basically two types of strings: unicode string and 8-bit string str. The latter contains byte data and common ASCII code data. in version X, the string is divided into bytes and str, both of which are immutable. Therefore, a variable bytearray is added.
In 2.x, a large number of methods of string type and str and unicode type are repeated. Therefore, the method of repeating with str in the string Module is not recommended in version 3.x. There are many useful constants and methods in the string Module, such as string. digits, which can be conveniently used in string encoding.
The 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])
We can see that, compared with the maketrans method of the string Module of 2. X, 3. X provides three static methods for creating the ing table.
Let's take a simple example to illustrate the process of string conversion:
Demonstration process in 2.x:
>>> Import string # import string Module >>> map = string. maketrans ('123', 'ABC') # Create a ing table and replace '1', '2', and '3' contained in the string with 'A ', 'B', 'C' >>> s = '000000' # string before conversion >>> s. translate (map) # Use the created ing table map to convert the string '54cbaabc789 '# The converted string
Demonstration process in 3.x:
>>> map = str.maketrans('123','abc') >>> s = '54321123789' >>> s.translate(map) '54cbaabc789'
2. X uses the maketrans function of string, and 3. X uses the maketrans function of str. In addition, the usage is basically the same. If the characters to be deleted in the specified string are used, they are slightly different, as shown below:
Demonstration process in 2.x:
>>> Import string >>> map = string. maketrans ('20140901', 'ABC') >>> s = '20160901' >>> s. translate (map, '78 ') # In addition to conversion, delete the '7' and '8' characters in the string. '54cbaabc9' # The converted string has no '7' characters ', '8'
Demonstration process in 3.x:
>>> Map = str. maketrans ('123', 'ABC', '78 ') # specify the characters to be deleted >>> s = '123' >>> s. translate (map) '54cbaabc9'
I encountered an example based on version 2.x in reading Python Cookbook, as shown below:
import string def translator(frm='', to='', delete='', keep=None): if len(to) == 1: to = to * len(frm) trans = string.maketrans(frm, to) if keep is not None: allchars = string.maketrans('', '') delete = allchars.translate(allchars, keep.translate(allchars,delete)) def translate(s): return s.translate(trans, delete) return translate
Allchars should be a returned ing table. Why can we call the translate method? so it should be of the str type. the test is as follows:
>>> import string >>> map = string.maketrans('123', 'abc') >>> type(map)
In version 3.x, this method cannot run normally. So where is the error? let's look at the type of the ing table:
>>> map = str.maketrans('123','abc') >>> type(map)
Knowing the type of the ing table, we can perform "post-processing" on it to meet our encoding requirements, just like in the above example of "Python Cookbook.
The example above uses a string consisting of ASCII characters. if it is of the byte type, 2. the operations in version X are the same. in array X, the bytes or bytearray function is called. if it is of the unicode type, 2. X requires 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 different. the reference manual shows that the transling table of the unicode translate method, that is, the dictionary key must be the unicode sequence number, and the value can be the unicode sequence number, unicode string, or None.