gpt4 book ai didi

python - 扩展插值在 configparser 中不起作用

转载 作者:太空狗 更新时间:2023-10-30 02:02:15 26 4
gpt4 key购买 nike

我尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com