Reputation: 1583
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)
Upvotes: 0
Views: 677
Reputation: 149776
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
Upvotes: 0
Reputation: 11961
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]'
Upvotes: 0