Created
February 5, 2018 17:37
-
-
Save turing4ever/0d60e852fda499db879574d4f69fccd4 to your computer and use it in GitHub Desktop.
Convert unicode into ascii recursively in nested object
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
def unicode_to_ascii(data): | |
""""Convert all unicode string into ascii string""" | |
if isinstance(data, basestring): | |
return str(data) | |
elif isinstance(data, collections.Mapping): | |
return dict(map(unicode_to_ascii, data.iteritems())) | |
elif isinstance(data, collections.Iterable): | |
return type(data)(map(unicode_to_ascii, data)) | |
else: | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment