How to use variables already defined in ConfigParser

前端 未结 3 342
余生分开走
余生分开走 2021-01-01 21:13

I\'m using ConfigParser in Python

config.ini is

[general]
name: my_name
base_dir: /home/myhome/exp

exe_dir: ${base_dir}/bin

Here I

3条回答
  •  清酒与你
    2021-01-01 21:39

    You can use ConfigParser interpolation

    On top of the core functionality, SafeConfigParser supports interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization.

    For example:

    [My Section] 
    foodir: %(dir)s/whatever 
    dir=frob 
    long: this value continues    
        in the next line 
    

    would resolve the %(dir)s to the value of dir (frob in this case). All reference expansions are done on demand.

    Your example becomes:

    [general]
    name: my_name
    base_dir: /home/myhome/exp
    
    exe_dir: %(base_dir)s/bin
    

提交回复
热议问题