How can I turn an INI file into a Dict?

Asked

Viewed 177 times

3

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

Is there any way to do this in Python?

[database]

host = localhost
password = sabe_nada_de_python
port = 3306

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"]
  • +1. good answer. I thought it was native but need to install.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.