Why in python giving str func will unicode string throw an exception?
2 answers
The call str()
on is the unicode
same as the call .encode(sys.getdefaultencoding())
on it. If unicode
contains characters that cannot be encoded in the default encoding, then it will throw UnicodeEncodeError
. The fix is to explicitly encode unicode
in a usable encoding eg 'utf-8'
.
+7
a source to share