我尝试在 python 3.6 或 python 3.5.1 中使用标准库中的 configparser 模块
我的 ini 文件如下所示:
[common]
domain = http://some_domain_name:8888
about = about/
loginPath = /accounts/login/?next=/home
fileBrowserLink = /filebrowser
partNewDirName = some_dir
[HUE_310]
partNewFilePath = ${common:domain}
我的“主要”程序如下所示:
from configparser import ConfigParser
parser = ConfigParser()
parser.read('configfile.ini')
lll = parser.get('HUE_310', 'partNewFilePath')
print(lll)
相反 http://some_domain_name:8888
我得到了 ${common:domain}
我使用 Pycharm Community 作为我的 IDE。我使用 virtualenv。
我不知道我的代码有什么问题...
最佳答案
如果您想要扩展插值,您必须创建 configparser.ExtendedInterpolation
类的实例,通过调用它,然后使用在创建 ConfigParser
实例时使用 interpolation=
关键字参数,如下所示:
from configparser import ConfigParser, ExtendedInterpolation
parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read('configfile.ini')
lll = parser.get('HUE_310', 'partNewFilePath')
print(lll) # -> http://some_domain_name:8888
关于python - 扩展插值在 configparser 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42839889/