[英]Python configure dict structure
Is there a way to save a python dict structure in config then access it? 有没有一种方法可以在配置中保存python dict结构然后访问它?
For example: 例如:
dict = {'root': {'category': {'item': 'test'}}}
# in my config
key = 'some string here'
print (dict[key])
# output
>> test
Solution thanks to answers below: 解决方案归功于以下答案:
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协议
Just use the pickle protocol. 只需使用pickle协议即可。 You can store virtually anything using that.
您几乎可以使用它存储任何东西。 documentation on pickling: http://docs.python.org/2/library/pickle.html
有关酸洗的文档: http : //docs.python.org/2/library/pickle.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.