def strip_non_ascii(string): ''' Returns the string without non ASCII characters''' stripped = (c for c in string if 0 < ord(c) < 127) return ''.join(stripped) test = u'éáé123456tgreáé@€' print test print strip_non_ascii(test)
Here is what the above code is Doing:
1. The strip_non_ascii function takes a string as an argument.
2. The stripped variable is a generator expression that iterates over the string and returns a character if it is between 0 and 127.
3. The join method is called on the stripped variable and returns a string.