Created
February 28, 2015 14:21
-
-
Save bcho/dd08494765c505b9e9f1 to your computer and use it in GitHub Desktop.
trick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unicodedata | |
| def must_be_ascii(raw): | |
| '''Convert all non-ascii characters to ascii substitutes. | |
| :param raw: raw input. | |
| ''' | |
| if not isinstance(raw, (str, unicode)): | |
| raise TypeError('Cannot convert to ascii, expected str or unicode.') | |
| if not isinstance(raw, unicode): | |
| raw = raw.decode('u8') | |
| return unicodedata.normalize('NFKD', raw) \ | |
| .encode('ascii', 'ignore') \ | |
| .decode('u8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment