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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s