Python / SQL — Remove all non ASCII characters from unicode string

Life

Here’s a brute force method to force a unicode string to ASCII by removing all non ascii characters from a unicode string for those pesky Ordinal Not In Range errors or DB errors.

Though I found comprehensive solutions that replace the non ASCII characters and such with approximate values, I just wanted to get rid of them, ASAP, Yesterday.

''.join([x for x in 'YOUR_STRING' if ord(x) < 128])
Advertisements

5 thoughts on “Python / SQL — Remove all non ASCII characters from unicode string

  1. Hi, thanks for this simple method to get rid of non-ASCII characters. I’m using urllib.quote and it literally throws up when it sees a unicode string.
    I think you mean “if ord(x) < 128]" though.

  2. raw = ”.join([i if ord(i) < 128 else ' ' for i in data]) #if you want to replace non-ancii char with space

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s