有没有办法在配置中保存 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