Skip to content Skip to sidebar Skip to footer

Convert A Unicode Object To A Latin String With Entities

I have a unicode object like x = u'a & 日本語: enči hallöle' and want to convert it into a latin-1 string with html-entities like 'a & 日本

Solution 1:

Use the "xmlcharrefreplace" option of unicode.encode, but note that it won't translate & to & for you:

>>> x = "a & 日本語:  enči hallöle".decode("utf-8")
>>> x.replace("&", "&").encode("latin-1", "xmlcharrefreplace")
'a & 日本語:  enči hall\xf6le'

Post a Comment for "Convert A Unicode Object To A Latin String With Entities"