您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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

from json import loads, dumps
from collections import OrderedDict

def to_dict(input_ordered_dict):
    return loads(dumps(input_ordered_dict))

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

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

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

(例如):

from collections import OrderedDict as odict
from json import loads, dumps

class 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 serializable

def custom_serializer(obj):
    if isinstance(obj, Name):
        return obj.__dict__

b = dumps(a) 
# Produces TypeError, as the Name objects are not serializable
b = 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, dumps
from collections import OrderedDict

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 also not a Name class object
        return None

def to_dict(input_ordered_dict):
    return loads(dumps(input_ordered_dict, default=custom_serializer))
其他 2022/1/1 18:34:15 有632人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶