My output looks like 'àéêöhello!'. I need change my output like this 'aeeohello', Just replacing the character à as a like this.
Asked
Active
Viewed 4.6k times
23
-
3Possible duplicate of [What is the best way to remove accents in a Python unicode string?](https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string) – hestellezg Jun 28 '18 at 17:44
3 Answers
46
Please Use the below code:
import unicodedata
def strip_accents(text):
try:
text = unicode(text, 'utf-8')
except NameError: # unicode is a default on python 3
pass
text = unicodedata.normalize('NFD', text)\
.encode('ascii', 'ignore')\
.decode("utf-8")
return str(text)
s = strip_accents('àéêöhello')
print s

Tomerikoo
- 18,379
- 16
- 47
- 61

Eswara Moorthy
- 469
- 3
- 5
-
1
-
2
-
Why `str(text)`? `normalize` seems to return a string(Python 3.9). – Safwan Samsudeen Dec 12 '20 at 03:53
-
7
-
Works without the block try (text = unicode(text, 'utf-8') gets an error. Thanks for the code. – erfelipe Jan 30 '21 at 00:19
12
import unidecode
somestring = "àéêöhello"
#convert plain text to utf-8
u = unicode(somestring, "utf-8")
#convert utf-8 to normal text
print unidecode.unidecode(u)
Output:
aeeohello

Tomerikoo
- 18,379
- 16
- 47
- 61

Alpesh Valaki
- 1,611
- 1
- 16
- 36
6
Alpesh Valaki's answer is the "nicest", but I had to do some adjustments for it to work:
# I changed the import
from unidecode import unidecode
somestring = "àéêöhello"
#convert plain text to utf-8
# replaced unicode by unidecode
u = unidecode(somestring, "utf-8")
#convert utf-8 to normal text
print(unidecode(u))

Tomerikoo
- 18,379
- 16
- 47
- 61

Samuel Lacombe
- 91
- 1
- 4
-
This works for me trying to get stuff like: 'Đà Nẵng' into 'Da Nang' - thanks! – g0h Oct 15 '22 at 08:08