I want to remove ALL symbols from my string efficiently.
x = hello!!
r = dict.fromkeys(map(ord, '\n ' + string.punctuation))
x.translate(r)
I expected this to remove all symbols instead of only a full stop (.)
I want to remove ALL symbols from my string efficiently.
x = hello!!
r = dict.fromkeys(map(ord, '\n ' + string.punctuation))
x.translate(r)
I expected this to remove all symbols instead of only a full stop (.)
What about using re.sub
to remove all string.punctuation
and ' \n'
:
x = re.sub('|'.join(map(re.escape, string.punctuation + ' \n')), '', x)
You could also use the following regex if you want to keep only alphabet and digit characters:
x = re.sub('[^a-zA-Z0-9]', '', x)