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

标签 python python-3.x configparser

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

相关文章:

python - QtGui.QApplication 和 QtCore.QCoreApplication 之间的区别

python-3.x - 如何为 BigQuery 的 Google Cloud 远程实例提供身份验证?

python - 在不完整/未填充的行中居中 matplotlib 图例条目?

python - 将 shell 命令的无限输出通过管道传输到 python 脚本

python - pygame.RESIZABLE 给出 KeyError

python - 如何知道我的标签在 python 中的混淆矩阵中正确排列?

python - 将字符串作为参数传递时出错(Solidity)

python - 在Python2.7中实现Barrier

python - 从python中的.ini文件读取特殊字 rune 本

Python Config 解析器读取注释和值