Skip to content

Instantly share code, notes, and snippets.

@lclibardi
Created March 26, 2018 19:40
Show Gist options
  • Save lclibardi/dd8c5e9b75cdb7a271c9965c949f6d72 to your computer and use it in GitHub Desktop.
Save lclibardi/dd8c5e9b75cdb7a271c9965c949f6d72 to your computer and use it in GitHub Desktop.
Converts all keys and values of a dictionary to ASCII strings
import unicodedata
def stringify_dictionary(dic):
"""
Converts all keys and values of a dictionary to ascii strings
"""
tmp = {}
for k, v in dic.iteritems():
# First transform all characters to unicode
key = unicode(k)
value = unicode(v)
# Now normalize all unicode characters
key = unicodedata.normalize('NFKD', key)
value = unicodedata.normalize('NFKD', value)
# Encode strings back to ascii
key = key.encode('ascii', 'ignore')
value = value.encode('ascii', 'ignore')
tmp[key] = value
return tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment