Created
August 30, 2015 22:40
-
-
Save tjwds/7db7b4f4f95b1ca38fa6 to your computer and use it in GitHub Desktop.
Comprehensive Unicode -> ASCII python function
This file contains 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
"""Alright, so it's pretty common knowledge that unicode and python just don't get along. Especially on OS X, apparently? | |
Separating out the sort function from the actual database that you're working with is, in my opinion, an elegant solution, | |
though maybe bad in big-O terms. Anyway, there are a few good options online, but if you don't want to install any libraries | |
(here, I need this particular function to be hyper-portable), you have to consider a lot of different circumstances. | |
Part of this function (the part that actually does something) comes from here: | |
http://stackoverflow.com/questions/2700859/how-to-replace-unicode-characters-by-ascii-characters-in-python-perl-script-giv""" | |
# be sure to import unicodedata | |
def fkunicode(culprit): | |
if isinstance(culprit, unicode): | |
culprit = unicodedata.normalize('NFKD', culprit).encode('ascii', 'ignore') | |
culprit = culprit.lower() | |
return culprit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment