Better string.translate?

Steve Purcell stephen_purcell at yahoo.com
Wed Mar 14 09:19:41 EST 2001


Stowasser Harald wrote:
> Hello,
> i am very new in Python. I don't know how i can do this better:
> 
> _deltable=string.maketrans('.,:;-_?!.=()´[]{}\'\"\r\n\f\t\v','
> ')
> ret = string.translate(s,_deltable)
> 
> This works fine, but i need a 'anti-maketrans'.
> I want to define what characters NOT to change in a space-character.


Use the 'sub' function in the 're' module:

>> import re
>> re.sub('[^abcd]','', "string from which to remove all but a, b, c or d")
'cababcd'


-Steve


-- 
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Any opinions expressed herein are my own and not necessarily those of Yahoo




More information about the Python-list mailing list