def _normalize(d):
'''
The above parse function generates output of list in dict form
i.e. {'abc' : {0: 'xyz', 1: 'pqr'}}. This function normalize it and turn
them into proper data type, i.e. {'abc': ['xyz', 'pqr']}
Note: if dict has element starts with 10, 11 etc.. this function won't fill
blanks.
for eg: {'abc': {10: 'xyz', 12: 'pqr'}} will convert to
{'abc': ['xyz', 'pqr']}
'''
newd = {}
if isinstance(d, dict) == False:
return d
# if dictionary. iterate over each element and append to newd
for k, v in six.iteritems(d):
if isinstance(v, dict):
first_key = next(iter(six.viewkeys(v)))
if isinstance(first_key, int):
temp_new = []
for k1, v1 in v.items():
temp_new.append(_normalize(v1))
newd[k] = temp_new
elif first_key == '':
newd[k] = v.values()[0]
else:
newd[k] = _normalize(v)
else:
newd[k] = v
return newd
评论列表
文章目录