Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

python text maketrans and translate


May 10, 2021 Python2


Table of contents


python text maketrans and translate

Scene:

Filter some characters of a string, and let's start with the example

  >>> tb=str.maketrans ('abc','123')  
  >>> 'abcd'.translate (tb)  
  '123d'  
  >>> 'abcd+++a+b+cd'.translate (tb)  
  '123d+++1+2+3d'  
  >>>   

1. Create a character map, which is what the maketrans method does, and it returns a mapping table of strings, which means that if a appears in the string, it becomes the corresponding 1, and so on, b-gt;2, c-gt;3

2. Use the translate method to remove the characters from the string

Attention:

1. The length of the mapping must be the same twice, otherwise the error must be reported, that is, 'abc' must be for '123' or '234', etc., can not be 'abc' corresponding to '12', this time the error

  >>> tb=str.maketrans ('abc','12')  
  Traceback (most recent call last):  
    File "", line 1in   
      tb=str.maketrans ('abc','12')  
  ValueError: the first two maketrans arguments must have equal length  
  >>>   

2. It is also important to note that the translate method of the string accepts only one argument, while the translate method of bytes can receive two parameters, and the second parameter can quickly implement the delete method

  >>> bytes_tb = bytes.maketrans(b'abcd'b'ABCD')  
  >>> b'abcdefg'.translate (bytes_tb,b'a')  
  b'BCDefg'  
  >>>   

Here are some examples of how to apply translate: (Note: For quick deletion, the following methods are all bytes' translate)

Modify some strings:

  >>> def AToB(seq,frm,to):  
      if len(frm)or len(frm)>len(to):return '映射字符长度不一致'  
      else:  
          bytes_tb = bytes.maketrans(frm,to)  
          return seq.translate (bytes_tb)  
    
        
  >>> AToB(b'abcd',b'a',b't1')  
  '映射字符长度不一致'  
  >>> AToB(b'abcd',b'a1',b't')  
  '映射字符长度不一致'  
  >>> AToB(b'abcd',b'a',b't')  
  b'tbcd'  
  >>>   

Remove some strings:

  >>> def AToB(seq,delete):  
      return seq.translate (None,delete)  
    
  >>> AToB(b'abcd',b'a')  
  b'bcd'  
  >>>   

Keep some characters:

  >>> def AToB(seq,keep):  
      delete=seq.translate (None,keep)  
      return seq.translate (None,"font-family: Arial, Helvetica, sans-serif;">delete)  
    
  >>> AToB(b'abcd',b'a')  
  b'a'  
  >>>