繁体   English   中英

Python配置字典结构

[英]Python configure dict structure

有没有一种方法可以在配置中保存python dict结构然后访问它?

例如:

dict = {'root': {'category': {'item': 'test'}}}

# in my config
key = 'some string here'

print (dict[key])

# output
>> test

解决方案归功于以下答案:

from functools import reduce
import json

dict = {'root': {'category': {'item': 'test'}}}

# you can put this in your config.ini file
map = '["root", "category", "item"]'

print (reduce(lambda d, k: d[k], json.loads(map), dict))

#output
test

尝试使用reduce()函数或pickle协议

只需使用pickle协议即可。 您几乎可以使用它存储任何东西。 有关酸洗的文档: http : //docs.python.org/2/library/pickle.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM