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

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])

5 Comments

  1. fgm2r says:

    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.

    1. Yuji says:

      You’re absolutely right 😀

  2. Thanks, worked great 🙂

  3. Tahir says:

    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 to Steven Callister Cancel reply