将嵌套字典键转换为带点字符串及其相应值的基本字典
dict-dot-notate的Python项目详细描述
口述
dict_dot_notate将嵌套的字典键及其相应值转换为带点字符串的基本字典。在
安装
pip install dict-dot-notate
使用
^{pr2}$示例
data = {
"obj": "obj",
"nested_once": {"first": "first"},
"nested_twice": {
"twice": {"inner_nest": {"second": "second"}}
},
"nested_thrice": {
"thrice": {"second_nest": {"tripple_nest": {"tripple": "thrice"}, "three": "three"}}
}
}
调用相关方法来转换dict
result = dict_dot_notate(data)
输出
{
'obj': 'obj',
'nested_once.first': 'first',
'nested_twice.twice.inner_nest.second': 'second',
'nested_thrice.thrice.second_nest.tripple_nest.tripple': 'thrice',
'nested_thrice.thrice.second_nest.three': 'three'
}
如果传入的dict有列表值,则输出如下
示例
data = {
"obj": "obj",
"nested_once": {"first": "first"},
"nested_twice": {
"twice": {"inner_nest": [1, 2, 3, 4, 5]}
}
}
转换
result = dict_dot_notate(data)
输出
{
'obj': 'obj',
'nested_once.first': 'first',
'nested_twice.twice.inner_nest': [1, 2, 3, 4, 5]
}
不传入词典列表。
- 项目
标签: