views:

121

answers:

5

Hi, I have some strings that I want to delete some unwanted characters from them. For example: Adam'sApple ----> AdamsApple.(case insensitive) Can someone help me, I need the fastest way to do it, cause I have a couple of millions of records that have to be polished. Thanks

+4  A: 

One simple way:

>>> s = "Adam'sApple"
>>> x = s.replace("'", "")
>>> print x
'AdamsApple'

... or take a look at regex substitutions.

The MYYN
+2  A: 

Try:

"Adam'sApple".replace("'", '')

One step further, to replace multiple characters with nothing:

import re
print re.sub(r'''['"x]''', '', '''a'"xb''')

Yields:

ab
xyld
+1  A: 
str.replace("'","");
Delan Azabani
+5  A: 

Any characters in the 2nd argument of the translate method are deleted:

>>> "Adam's Apple!".translate(None,"'!")
'Adams Apple'

NOTE: translate requires Python 2.6 or later to use None for the first argument, which otherwise must be a translation string of length 256. string.maketrans('','') can be used in place of None for pre-2.6 versions.

Mark Tolonen
I might be helpful to explicitly mention `string.maketrans('', '')` as a substitute for `None` for Python < 2.6
J.F. Sebastian
@JF, added, thanks.
Mark Tolonen
Six times faster than `"".join(char for char in text if char not in bad_chars)` :)
badp
+1  A: 
voyager