如何将嵌套的OrderedDict转换为dict?

如何将嵌套的OrderedDict转换为dict?,第1张

如何将嵌套的OrderedDict转换为dict?

最简单的解决方案是使用json转储和加载

from json import loads, dumpsfrom collections import OrderedDictdef to_dict(input_ordered_dict):    return loads(dumps(input_ordered_dict))

注意:上面的代码适用于被json称为可序列化对象的字典。可以在此处找到默认对象类型的列表

因此,如果有序词典不包含特殊值,这应该就足够了。

编辑:基于注释,让我们改进上面的代码。让我们说,input_ordered_dict可能包含默认情况下无法通过json序列化的自定义类对象。在这种情况下,我们应该将default参数json.dumps与我们的自定义序列化程序一起使用。

(例如):

from collections import OrderedDict as odictfrom json import loads, dumpsclass Name(object):    def __init__(self, name):        name = name.split(" ", 1)        self.first_name = name[0]        self.last_name = name[-1]a = odict()a["thiru"] = Name("Mr Thiru")a["wife"] = Name("Mrs Thiru")a["type"] = "test" # This is by default serializabledef custom_serializer(obj):    if isinstance(obj, Name):        return obj.__dict__b = dumps(a) # Produces TypeError, as the Name objects are not serializableb = dumps(a, default=custom_serializer)# Produces desired output

此示例可以进一步扩展到更大的范围。我们甚至可以根据需要添加过滤器或修改值。只需在custom_serializer函数中添加其他部分

def custom_serializer(obj):    if isinstance(obj, Name):        return obj.__dict__    else:        # Will get into this if the value is not serializable by default         # and is not a Name class object        return None

如果是自定义序列化程序,则顶部给出的功能应为:

from json import loads, dumpsfrom collections import OrderedDictdef custom_serializer(obj):    if isinstance(obj, Name):        return obj.__dict__    else:        # Will get into this if the value is not serializable by default         # and is also not a Name class object        return Nonedef to_dict(input_ordered_dict):    return loads(dumps(input_ordered_dict, default=custom_serializer))


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5663109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存