Remove Special Chracters From A String In Django
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 f
Solution 1:
Why not use .replace()
?
eg.
a='testemail@email.com'
a.replace('@','_')
'testemail_email.com'
and to edit multiple you can probably do something like this
a='testemail@email.com'
replace=['@','.']
foriin replace:
a=a.replace(i,'_')
Solution 2:
Take this as a guide:
import re
a = re.sub(u'[@]', '"', a)
SYNTAX:
re.sub(pattern, repl, string, max=0)
Solution 3:
Great example from Python Cookbook 2nd edition
importstring
def translator(frm='', to='', delete='', keep=None):
iflen(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("me-and_you@gmail.com")
output:
me~and~you~gmail~com
A great string util to put in your toolkit.
All credit to the book
Post a Comment for "Remove Special Chracters From A String In Django"