问题描述
我正在努力解决以下问题:我想像这样转换 ordereddict:
i am struggling with the following problem: i want to convert an ordereddict like this:
ordereddict([('method', 'constant'), ('data', '1.225')])
到这样的常规字典中:
{'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.
感谢任何提示或百家乐凯发k8的解决方案,
thanks for any hint or solutions,
本
推荐答案
>>> from collections import ordereddict >>> ordereddict([('method', 'constant'), ('data', '1.225')]) ordereddict([('method', 'constant'), ('data', '1.225')]) >>> dict(ordereddict([('method', 'constant'), ('data', '1.225')])) {'data': '1.225', 'method': 'constant'} >>>
但是,要将其存储在数据库中,最好将其转换为 json 或 pickle 等格式.使用 pickle,您甚至可以保持订单!
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!