是否可以从另一个部分引用 ini 中的变量?
我知道你可以执行以下操作
[env]
dir = /home/a
dir2 = %(dir)s/b
但是,如果我有两个部分并且想要引用该部分中的变量,会发生什么情况?
[env]
name = DEV
[dir]
home = /home/<name from env here>/scripts
Thanks
请参阅有关的文档configparser https://docs.python.org/library/configparser.html#interpolation-of-values。创建具有扩展插值的解析器。使用${section:option}
引用其他部分的选项的语法。
from configparser import ConfigParser, ExtendedInterpolation
parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read_string('''[env]
name = DEV
[dir]
home = /home/${env:name}/scripts
''')
print(parser['dir']['home'])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)