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])
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.
You’re absolutely right 😀
Thanks, worked great 🙂
raw = ”.join([i if ord(i) < 128 else ' ' for i in data]) #if you want to replace non-ancii char with space