Reputation: 1736
I want to remove all special characters from email such as '@', '.' and replace them with 'underscore' there are some functions for it in python 'unidecode' but it does not full fill my requirement . can anyone suggest me some way so that I can find the above mention characters in a string and replace them with 'underscore'.
Thanks.
Upvotes: 3
Views: 6396
Reputation: 4903
Great example from Python Cookbook 2nd edition
import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate
remove_cruft = translator(frm="@-._", to="~")
print remove_cruft("[email protected]")
output:
me~and~you~gmail~com
A great string util to put in your toolkit.
All credit to the book
Upvotes: 1
Reputation: 2738
Why not use .replace()
?
eg.
a='[email protected]'
a.replace('@','_')
'testemail_email.com'
and to edit multiple you can probably do something like this
a='[email protected]'
replace=['@','.']
for i in replace:
a=a.replace(i,'_')
Upvotes: 5
Reputation: 22808
Take this as a guide:
import re
a = re.sub(u'[@]', '"', a)
SYNTAX:
re.sub(pattern, repl, string, max=0)
Upvotes: 1