@Moinuddin Quadri's answer is better suited to your use case, but in general a simple way to remove non-ASCII characters from a given string is to do the following:
# the characters '¡' and '¢' are non-ASCII string = "hello, my name is ¢arl... ¡Hola!" all_ascii = ''.join(char for char in string if ord(char) < 128)
This leads to:
>>> print(all_ascii) "hello, my name is arl... Hola!"
You can also do this:
''.join(filter(lambda c: ord(c) < 128, string))
But this is about 30% slower than the char for char ...
approach.
source share