Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 如何使用已在 ConfigParser 中定义的变量
例如,我有以下配置文件:
[Sec1] opt1 = 1 [Sec2] opt2 = 1
我想明确设置opt2等于opt1,这样如果我opt1稍后更新,opt2也会更新。
opt2
opt1
但我找不到办法做到这一点。
您可以将插值与ConfigParser或一起使用SafeConfigParser。但是,您不能在配置中的部分之间进行插值,除非其中之一是DEFAULT:
ConfigParser
SafeConfigParser
DEFAULT
[DEFAULT] opt1 = 1 [Sec2] opt2 = %(opt1)s
请务必注意,这些DEFAULT选项会掩盖在其他部分中使用它们的任何进一步尝试,并且您不能通过 重新分配opt1。Sec2.opt2
Sec2.opt2