def array_to_utf(a):
autf = []
i = 0
for v in a:
if isinstance(v, unicode):
autf.append(v.encode('utf-8'))
elif isinstance(v, dict):
autf.append(dict_to_utf(v))
elif isinstance(v, list):
autf.append(array_to_utf(v))
else:
autf.append(v)
return autf
def dict_to_utf(d):
dutf = {}
for k,v in d.iteritems():
if isinstance(v, unicode):
dutf[k] = v.encode('utf-8')
elif isinstance(v, list):
dutf[k] = array_to_utf(v)
elif isinstance(v, dict):
dutf[k] = dict_to_utf(v)
else:
dutf[k] = v
return dutf
test = {1: u'1', 2: '2', 3: {'x': u'x', 'y': 'y'}, 4: [u'ara', 's', 123], 5: 123}
print(dict_to_utf(a))
# {1: '1', 2: '2', 3: {'y': 'y', 'x': 'x'}, 4: ['ara', 's', 123], 5: 123}
Created
April 12, 2016 13:51
-
-
Save RafaAguilar/754c9f5b60a44f1e75bc9127dac52bf5 to your computer and use it in GitHub Desktop.
Python: Convert a complex dict with possible UTF values into a regular STR one
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment