9

Is it possible to store a multi-dimensional dictionary (3 deep) using the Python 'configparser' using indentions? The work-around is to split the key values, but wanted to know if there was a clean way to import directly into a dictionary.

DOES NOT WORK - USING SUB-OPTION INDENTION IN CONFIGPARSER


    [OPTIONS]
        [SUB-OPTION]
        option1 = value1
        option2 = value2
        option3 = value3

WORKS - SPLITING USED ON SUB-OPTION VALUES


    [OPTIONS]
    SUB-OPTION  = 'option1, value1',    
                  'option2, value2',
                  'option3, value3'

DICTIONARY VALUES


    dict['OPTIONS']['SUB-OPTION'] = {
        option1 : value1,
        option2 : value2,
        option3 : value3,
    }

NobleVision
  • 107
  • 1
  • 8

3 Answers3

5

ASAIK, there is a nested configuration file in that format.

I suggest a json like config file:

{
 "OPTIONS": {
   "SUB-OPTIONS": {
     "option1" : value1,
     "option2" : value2,
     "option3" : value3,
   }
 }
}

Then in the code use:

from ast import literal_eval
with open("filename","r") as f:
 config = literal_eval(f.read())

Edit

Alternatively, you can use YAML (with PyYAML) as a great configuration file.

The following configuration file:

option1:
    suboption1:
        value1: hello
        value2: world
    suboption2:
        value1: foo
        value2: bar

Can be parsed using:

import yaml
with open(filepath, 'r') as f:
    conf = yaml.safe_load(f)

Then you can access the data as you would in a dict:

conf['option1']['suboption1']['value1']
>> 'hello'
Asclepius
  • 57,944
  • 17
  • 167
  • 143
Liran Funaro
  • 2,750
  • 2
  • 22
  • 33
  • Thanks, was trying to have a more human readable configuration file with the built-in Python configparser, but looks like JSON is the only built-in solution. YAML may be another option. – NobleVision Jun 22 '16 at 13:04
  • Then I'm guessing that the easiest way arround it is just using the following format: [OPTION,SUBOPTION] option1=value1 option2=value2 option3=value3 Then parse it using configparse it and splitting every category name according to commas and buid the dictionery as you mentioned. – Liran Funaro Jun 23 '16 at 02:34
  • YAML might also be wroth a look – Liran Funaro Apr 27 '17 at 13:26
  • I found a Python package [ConfigObj](http://configobj.readthedocs.io/en/latest/configobj.html) that has nested sections, but the last time it was maintained was in 2014. – NobleVision Dec 29 '17 at 02:18
2

config.ini

OPTIONS  = {"option1": "value1", "option2": "value2", "option3": "value3"}

Code:

import json
options = json.loads(conf['OPTIONS'])
Daniel Braun
  • 2,452
  • 27
  • 25
0

As others have pointed out: the requested nesting capability is not supported by the current configparser implementation.

However, TOML configuration files follow a similar syntax to INI files and allow for nested structures. See the official specification here and the corresponding python library here.

You may also checkout this question on SO with the following example:

name = "A Test of the TOML Parser"

[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""

[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]]
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 7
[[things.objs.morethings]]
y = [
    2,
    3,
    4
]
[[things.objs.morethings]]
y = 9

[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""

JSON output:

{
    "name": "A Test of the TOML Parser",
    "things": [{
        "a": "thing1",
        "b": "fdsa",
        "multiLine": "Some sample text."
    }, {
        "a": "Something else",
        "b": "zxcv",
        "multiLine": "Multiline string",
        "objs": [{
            "x": 1
        }, {
            "x": 4
        }, {
            "x": 7,
            "morethings": [{
                "y": [2, 3, 4]
            }, {
                "y": 9
            }]
        }]
    }, {
        "a": "3",
        "b": "asdf",
        "multiLine": "thing 3.\\nanother line"
    }]
}
Mr.Epic Fail
  • 358
  • 3
  • 15