Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all the contents in ini file into dictionary with Python

Normally, I code as follows for getting a particular item in a variable as follows

try:     config = ConfigParser.ConfigParser()     config.read(self.iniPathName) except ConfigParser.MissingSectionHeaderError, e:     raise WrongIniFormatError(`e`)  try:     self.makeDB = config.get("DB","makeDB") except ConfigParser.NoOptionError:     self.makeDB = 0 

Is there any way to read all the contents in a python dictionary?

For example

 [A] x=1 y=2 z=3 [B] x=1 y=2 z=3 

is written into

 val["A"]["x"] = 1 ... val["B"]["z"] = 3 
like image 468
prosseek Avatar asked Jul 10 '10 20:07

prosseek


People also ask

How do I read an INI file in python?

To read and write INI files, we can use the configparser module. This module is a part of Python's standard library and is built for managing INI files found in Microsoft Windows. This module has a class ConfigParser containing all the utilities to play around with INI files. We can use this module for our use case.

How do I read a .ini file?

How to Open and Edit INI Files. It's not a common practice for people to open or edit INI files, but they can be opened and changed with any text editor. Just double-clicking it will automatically open it in the Notepad application in Windows.

How parse config file in python?

Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.


2 Answers

I suggest subclassing ConfigParser.ConfigParser (or SafeConfigParser, &c) to safely access the "protected" attributes (names starting with single underscore -- "private" would be names starting with two underscores, not to be accessed even in subclasses...):

import ConfigParser  class MyParser(ConfigParser.ConfigParser):      def as_dict(self):         d = dict(self._sections)         for k in d:             d[k] = dict(self._defaults, **d[k])             d[k].pop('__name__', None)         return d 

This emulates the usual logic of config parsers, and is guaranteed to work in all versions of Python where there's a ConfigParser.py module (up to 2.7, which is the last of the 2.* series -- knowing that there will be no future Python 2.any versions is how compatibility can be guaranteed;-).

If you need to support future Python 3.* versions (up to 3.1 and probably the soon forthcoming 3.2 it should be fine, just renaming the module to all-lowercase configparser instead of course) it may need some attention/tweaks a few years down the road, but I wouldn't expect anything major.

like image 53
Alex Martelli Avatar answered Sep 28 '22 03:09

Alex Martelli


I managed to get an answer, but I expect there should be a better one.

dictionary = {} for section in config.sections():     dictionary[section] = {}     for option in config.options(section):         dictionary[section][option] = config.get(section, option) 
like image 43
prosseek Avatar answered Sep 28 '22 04:09

prosseek