Convert a Unicode string to a string in Python (containing extra symbols)


Question

How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?

1
476
3/22/2016 5:05:28 PM

Accepted Answer

title = u"Klüft skräms inför på fédéral électoral große"
import unicodedata
unicodedata.normalize('NFKD', title).encode('ascii','ignore')
'Kluft skrams infor pa federal electoral groe'
545
7/30/2009 3:44:32 PM

You can use encode to ASCII if you don't need to translate the non-ASCII characters:

>>> a=u"aaaàçççñññ"
>>> type(a)
<type 'unicode'>
>>> a.encode('ascii','ignore')
'aaa'
>>> a.encode('ascii','replace')
'aaa???????'
>>>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon