removing characters from a string?

Hirsch, John JohnH at PHM.GOV.AU
Mon Jul 31 20:27:32 EDT 2000


>say I had a string. 


>this_string = "jesse at multimediacollective.com"

>"""and I wanted to get rid of the @, how would I do that? This 
>process has to be efficent for removing large quantities of special 
>characters, such as <,>,#,%,* etc, from the same string.  

>Any advice would be very helpful.  Thanks in advance, Jesse. 

You could do this, not sure how fast or slow it is.
>>> 
>>> import re
>>> this_string = "jesse at multimediacollective.com"
>>> pattern = re.compile('\W')
>>> new_line = re.sub(pattern, '', this_string)
>>> new_line
'jessemultimediacollectivecom'
>>> 

It will remove all non alphanumeric characters from a string.
For more info on re have a look at the Regular Expression HOWTO
http://www.python.org/doc/howto/regex/regex.html

john
johnh at phm.gov.au





More information about the Python-list mailing list