Delete specified symbol string (not position)

remove the string in python some unwanted characters:

1, can be used generally replace ()

  This function is not limited to the position, you can replace the original character of unwanted, replaced with a null character equivalent of Deleted

 

2, can also be used strip (), delete characters on both sides (the default is to remove about blank)

  rstrip (), lstrip () these two can choose to delete only the left or right

 

3、re.sub

  This can be deleted based on the positive here is to remove the string of numbers 1-9, characters az, AZ, you can also add other

Import Re 

STR = " aksj2343ngr4545g gold leaf FG " 
TEMP = the re.sub ( ' [A-zA-Z1-9] ' , '' , STR)
 Print (TEMP)

4, the mapping can also be used

  

from String Import maketrans    # must call maketrans function. 

intab = " aeiou " 
outtab = " 12345 " 
trantab = maketrans (intab, outtab) 
# established here a map
= S 'abc123def456ghi789zero0' 
RES = s.translate (trantab)
# aeiou used herein, map the converted string 12345

 

Guess you like

Origin www.cnblogs.com/51python/p/11259486.html