python2有序字典为
from collections import OrderedDict
od = OrderedDict()
python3.6以后的dict默认就是有序的,所以这里的方案是使用python3.7将源字典生成新的逻辑:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
A = {"MC_SYNCMODE":"112",
"AAA":[12,34,],
"CVCC":{"SDSD":{"dwqd":"eee"}}}
print("od = OrderedDict()")
def SetOrderedDict(dict1,preName):
for key in dict1:
if type(dict1[key]) == dict:
print('{}["{}"]'.format(preName,key) + " = OrderedDict()")
SetOrderedDict(dict1[key],'{}["{}"]'.format(preName,key))
elif type(dict1[key]) == str:
print('{}["{}"]'.format(preName,key) + " = " + '"{}"'.format(dict1[key]))
else:
print('{}["{}"]'.format(preName,key) + " = " + '{}'.format(dict1[key]))
SetOrderedDict(A,"od")
输出:
od = OrderedDict()
od["MC_SYNCMODE"] = "112"
od["AAA"] = [12, 34]
od["CVCC"] = OrderedDict()
od["CVCC"]["SDSD"] = OrderedDict()
od["CVCC"]["SDSD"]["dwqd"] = "eee"