Is there a way to save a python dict structure in config then access it?
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. You can store virtually anything using that. documentation on pickling: http://docs.python.org/2/library/pickle.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.