How to convert an OrderedDict into a regular dict in python3

后端 未结 8 599
天命终不由人
天命终不由人 2020-12-04 09:43

I am struggling with the following problem: I want to convert an OrderedDict like this:

OrderedDict([(\'method\', \'constant\'), (\'data\', \'1.         


        
相关标签:
8条回答
  • 2020-12-04 10:16

    If you are looking for a recursive version without using the json module:

    def ordereddict_to_dict(value):
        for k, v in value.items():
            if isinstance(v, dict):
                value[k] = ordereddict_to_dict(v)
        return dict(value)
    
    0 讨论(0)
  • 2020-12-04 10:17

    Its simple way

    >>import json 
    >>from collection import OrderedDict
    
    >>json.dumps(dict(OrderedDict([('method', 'constant'), ('data', '1.225')])))
    
    0 讨论(0)
提交回复
热议问题