Skip to content

Instantly share code, notes, and snippets.

@abarnert
Last active March 27, 2018 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abarnert/26c44c41747ec5e380bba848ed982673 to your computer and use it in GitHub Desktop.
Save abarnert/26c44c41747ec5e380bba848ed982673 to your computer and use it in GitHub Desktop.
def deep_encode(obj, encoding):
if isinstance(obj, unicode):
return obj.encode(encoding)
elif isinstance(obj, dict):
return {deep_encode(key, encoding): deep_encode(value, encoding) for key, value in obj.items()}
elif isinstance(obj, list):
return [deep_encode(item, encoding) for item in obj]
else:
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment