Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace non-ascii chars from a unicode string in Python

Tags:

python

ascii

How can I replace non-ascii chars from a unicode string in Python?

This are the output I spect for the given inputs:

música -> musica

cartón -> carton

caño -> cano

Myaybe with a dict where 'á' is a key and 'a' a value?

like image 922
Juanjo Conti Avatar asked Sep 13 '10 21:09

Juanjo Conti


1 Answers

If all you want to do is degrade accented characters to their non-accented equivalent:

>>> import unicodedata
>>> unicodedata.normalize('NFKD', u"m\u00fasica").encode('ascii', 'ignore')
'musica'
like image 69
llasram Avatar answered Sep 24 '22 13:09

llasram