0
votes

I have a string in the formemail [ à ] example.com

I want to make it [email protected].

I tried :

print email.replace(u"\xa0", "@")
print email.replace(" [ à ] ", "@")
print email.replace(" à ", "@")
email = email.replace(u" à ", "@")

but I always get this error:

'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

2

2 Answers

0
votes

Alternatively, if you don't want to use unicode strings, use:

In [8]: email = 'email [ à ] domain.fr'

In [9]: email.replace(' [ \xc3\xa0 ] ', '@')
Out[9]: '[email protected]'
0
votes

It works if you use the unicode type for both the string and replacement:

>>> email = u"email [ à ] domain.fr"
>>> email.replace(u" [ à ] ", u"@")
u'[email protected]'

To get a unicode object out of str use .decode():

email.decode("utf-8")  # or provide another encoding