8

I'm using config.ini file to store all my configurations.

I need to store a dictionary and a list in the config file and parse it in my main.py file using configparser. Can anyone please tell me how do I go about doing that?

config.ini:

[DEFAULT] 
ADMIN = xyz 
SOMEDICT = {'v1': 'k1', 'v2': 'k2'}
SOMELIST = [v1, v2]

main.py:

config = configparser.ConfigParser() 
config.read('config.ini') 
secret_key = config['DEFAULT']['ADMIN']

If there is no way to do this, is config in json format a good option?

90abyss
  • 7,037
  • 19
  • 63
  • 94

4 Answers4

23

ConfigParser will only ever give you those elements as strings, which you would then need to parse.

As an alternative, YAML is a good choice for configuration files, since it is easily human readable. Your file could look like this:

DEFAULT:
    ADMIN: xyz
    SOMEDICT:
        v1: k1
        v2: k2
    SOMELIST:
        - v1
        - v2

and the Python code would be:

import yaml
with open('config.yml') as c:
    config = yaml.load(c)
config['DEFAULT']['SOMEDICT']
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • this really helps. apologies for dumb qs but could you tell me if there is an easy way to get it in the python file in dictionary format? i mean i need list to be list and dict to be dict while getting settings from config file. – 90abyss Aug 29 '17 at 21:37
  • I'm sorry, I don't understand what you're asking. The above code gives you list and dict format. – Daniel Roseman Aug 29 '17 at 21:39
  • this is what i get when i print(config['DEFAULT']['SOMEDICT']) -> v1:k1 v2:k2 ... what i need is {'v1': 'k1', 'v2': 'k2'} – 90abyss Aug 29 '17 at 21:43
  • When you use the code that I have given above, the result of `print(config['DEFAULT']['SOMEDICT'])` is `{'v1': 'k1', 'v2': 'k2'}`. – Daniel Roseman Aug 29 '17 at 21:45
  • 1
    But that's irrelevant anyway; if all you wanted to do was print it, then your original code which gives the result as a string would work fine. – Daniel Roseman Aug 29 '17 at 21:46
  • i printed it just as an example although i do have more to do with it. i will check what is causing the formatting issue while printing from YML file. thanks a lot for your help! – 90abyss Aug 29 '17 at 21:52
3

I would suggest to use json:

json.loads('[1, 2]') #=> [1, 2]
json.dumps([1, 2]) #=> '[1, 2]'
json.loads('{"v1": "k1", "v2": "k2"}') #=> {'v1': 'k1', 'v2': 'k2'}
json.dumps({'v1': 'k1', 'v2': 'k2'}) #=> '{"v1": "k1", "v2": "k2"}'

You will need to do dumps before saving and loads after reading for those fields you use JSON for.

Better solution would be to use JSON for the entire configuration file:

{
  "DEFAULT": {
    "ADMIN": "xyz",
    "SOMEDICT": {
      "v1": "k1",
      "v2": "k2"
    },
    "SOMELIST": [
      "v1",
      "v2"
    ]
  }
}

Then you could do:

conf = json.load(open('conf.json'))
json.dump(conf, open('conf.json', 'w'))
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
3

A JSON file with this data could look like this:

{
  "DEFAULT": {
    "ADMIN": "xyz",
    "SOMEDICT": {
      "v1": "k1",
      "v2": "k2"
    },
    "SOMELIST": [
      "v1",
      "v2"
    ]
  }
}

Then in python:

import json

with open('config.json') as f:
    config = json.load(f)
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

It is possible to store and read Dict or List data from an ini file

after reading the value you just have to use the built in eval( ) function, by default the variable will be of type string eval will check and convert it to the dictionary data type.

You can try the following code

dict_data = configur.get("DEFAULT", "SOMEDICT")
dict_data = eval(dict_data)

print(dict_data['v1'])
Harshal Deore
  • 1,050
  • 1
  • 11
  • 11