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
views:
121answers:
5
+4
A:
One simple way:
>>> s = "Adam'sApple"
>>> x = s.replace("'", "")
>>> print x
'AdamsApple'
... or take a look at regex substitutions.
The MYYN
2010-05-06 12:09:29
+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
2010-05-06 12:09:36
+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
2010-05-06 14:20:56
I might be helpful to explicitly mention `string.maketrans('', '')` as a substitute for `None` for Python < 2.6
J.F. Sebastian
2010-05-06 14:26:49
@JF, added, thanks.
Mark Tolonen
2010-05-06 14:31:38
Six times faster than `"".join(char for char in text if char not in bad_chars)` :)
badp
2010-05-06 16:22:47