【问题标题】:How to store formatted strings in a configuration file?如何将格式化的字符串存储在配置文件中?
【发布时间】:2014-12-12 05:47:20
【问题描述】:

我在 Python 2.6 中使用ConfigParser 模块,我希望从我的配置文件中加载格式化字符串。此类字符串的示例如下:

[Section Name]
#in the config file
#tmp_string has value 'string'
my_string = 'This is a %s' % tmp_string

有没有一种很好的方法可以将这些字符串保存在配置文件中?在加载配置文件之前,我不会知道字符串的值是什么,所以我无法评估它然后将其保存到文件或类似的东西中。显然,此刻,当我加载字符串并打印出来时,我得到以下信息:

#config is the ConfigParser
my_string = config.get('Section Name', 'my_string')
print my_string
>>>'This is a %s' % tmp_string 

我很想得到输出:

>>> This is a string

你是如何做到这一点的?我更愿意留在ConfigParser,但另一种选择可能是可以接受的。 (我知道你不能只打印字符串并让它以你想要的方式神奇地出现,我只是想展示我想要做什么。)

【问题讨论】:

    标签: python string-formatting python-2.x configuration-files configparser


    【解决方案1】:

    如果tmp_string 定义在配置文件的同一部分,那么您可以在同一部分的其他地方使用特殊格式引用它:%(variable_name)s

    因此将其应用于您的示例:

    myconfig.cfg文件

    [Section Name]
    tmp_string = string  ; must be in the same section
    my_string = This is a %(tmp_string)s
    

    现在tmp_string 的值将是“iterpolated”或在检索时替换为my_string 的值:

    try:
        from ConfigParser import ConfigParser
    except ModuleNotFoundError:
        from configparser import ConfigParser  # Python 3
    
    
    config = ConfigParser()
    print(config.get('Section Name', 'my_string'))
    

    输出:

    This is a string
    

    注意:通常不能引用当前部分之外的值。但是,如果它们是在名为 DEFAULT 的特殊部分中定义的,则它们的值可以。换句话说,以下配置文件会产生相同的结果:

    [DEFAULT]
    tmp_string = string
    
    [Section Name]
    my_string = This is a %(tmp_string)s
    

    如果在DEFAULT 部分和同一部分中都定义了一个名称作为对其的引用,则同一部分中的“本地”值优先并且将被使用。

    Python 3 变化

    ConfigParser 模块 已重命名为configparser,并且通过添加的新ExtendedInterpolation 类可以使用实现更高级语法的替代处理程序。要启用它,您需要创建它的一个实例并在实例化 ConfigParser 类时使用它——这可以通过同时执行这两个操作非常简洁:

    config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    

    这种形式的插值接受格式为:${section:variable_name} 的字符串。除了比%(variable_name)s 更易读之外,查找的变量可以在任何 指定的部分中,而不仅仅是一个名为DEFAULT 的特殊部分。如果section: 部分被省略,则变量名必须在当前部分(如果有,则为特殊的DEFAULT 部分)。

    【讨论】:

      【解决方案2】:

      同理the docs已经解释过了:

      my_string='This is a %(tmp_string)s'
      

      【讨论】:

        【解决方案3】:

        现在docs 还指定了一种使用${} 作为格式化程序的方法,我发现它更易于阅读和使用,而不是%()s。 这很容易实现:

        考虑以下配置文件,命名为.configfile

        [arbitrarySectionName]
        tmp_string = any section
        
        [Section Name]
        my_string = Now you can read from ${arbitrarySectionName:tmp_string} not just DEFAULT
        

        为了正确解释这一点,您将在您的模块中进行:

        import configparser
        config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
        config.read('.configfile')
        print(config.get('Section Name', 'my_string'))
        

        输出:

        现在您可以阅读任何部分,而不仅仅是 DEFAULT

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多