Python – Django — UnicodeDecodeError Force Unicode to ASCII

Python ❤ and Unicode is often a problem. Many libraries don't take unicode, and if your data contains unicode, python will complain loudly.

My "quick and dirty" solution thus far has been to do ''.join([x for x in mystring if ord(x) < 128]) – turns out there's a better one!

Use the string method encode with the second argument being "replace" which will replace errors with ?.

u'Hello\u2019'.encode('ascii','replace')
# out: Hello?

Leave a Comment