2

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.

Inforian
  • 1,716
  • 5
  • 21
  • 42

3 Answers3

5

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=['@','.']
for i in replace:
  a=a.replace(i,'_')
Jonathan
  • 2,728
  • 10
  • 43
  • 73
  • but replace takes only two arguments txt.replace('@','_' ) what if I want to remove more then one character – Inforian Feb 07 '13 at 09:32
1

Take this as a guide:

import re
a = re.sub(u'[@]', '"', a)

SYNTAX:

re.sub(pattern, repl, string, max=0)
catherine
  • 22,492
  • 12
  • 61
  • 85
  • Hi Cathy, replace is more fast than sub – Jonathan Feb 07 '13 at 09:25
  • thanks and it works but in opposite way...it is replacing **email@gmail.com** to **"""""@"""""."""** – Inforian Feb 07 '13 at 09:26
  • 1
    Have a look at [this](http://stackoverflow.com/questions/5668947/python-string-replace-vs-re-sub) – Jonathan Feb 07 '13 at 09:27
  • @Jonathan yes it works , but replace takes only two arguments **txt.replace('@','_' )** what if I want to remove more then one character – Inforian Feb 07 '13 at 09:31
  • 2
    Depending on what @Inforian wants to do, rather than specify a character class containing "special" characters, it might be easier to specify a negated character class of "non-special characters". For instance, if we define a "special character" as anything that isn't a Roman letter or number, then something like `re.sub(r'[^a-zA-Z0-9]', '_', email)`, should transform `an-example@example.com` into `an_example_example_com`. – Daisy Leigh Brenecki Feb 07 '13 at 09:34
1

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("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

sotapme
  • 4,695
  • 2
  • 19
  • 20