Created
March 26, 2018 19:40
-
-
Save lclibardi/dd8c5e9b75cdb7a271c9965c949f6d72 to your computer and use it in GitHub Desktop.
Converts all keys and values of a dictionary to ASCII strings
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 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