I am struggling with the following problem:
I want to convert an OrderedDict
like this:
OrderedDict([('method', 'constant'), ('data', '1.225')])
into a regular dict like this:
{'method': 'constant', 'data':1.225}
because I have to store it as string in a database. After the conversion the order is not important anymore, so I can spare the ordered feature anyway.
Thanks for any hint or solutions,
Ben
If you are looking for a recursive version without using the
json
module:Here is what seems simplest and works in python 3.7
However, to store it in a database it'd be much better to convert it to a format such as JSON or Pickle. With Pickle you even preserve the order!
Even though this is a year old question, I would like to say that using
dict
will not help if you have an ordered dict within the ordered dict. The simplest way that could convert those recursive ordered dict will beIt is easy to convert your
OrderedDict
to a regularDict
like this:If you have to store it as a string in your database, using JSON is the way to go. That is also quite simple, and you don't even have to worry about converting to a regular
dict
:Or dump the data directly to a file:
Its simple way