How can I turn an INI file into a Dict?

3

How can I transform data from a .ini file into a dict into Python?

Is there any way to do this in Python ?

[database]

host = localhost
password = sabe_nada_de_python
port = 3306
    
asked by anonymous 30.09.2016 / 20:09

1 answer

4

Yes, using configparser :

import configparser
import os

configuracao = configparser.ConfigParser()
configuracao.read(os.path.join(os.getcwd(), "meuarquivo.ini"))
host = configuracao["database"]["host"]
password = configuracao["database"]["password"]
port = configuracao["database"]["port"]
    
30.09.2016 / 20:19