Python configobj

From wikinotes

Configobj is a python module that read/writes an improved .ini file format (which supports nesting).


WARNING:

configobj is deprecated in favour of python configparser

Documentation

official docs https://configobj.readthedocs.io/en/latest/
github https://github.com/DiffSK/configobj

Format

## comments are allowed
[A]
test = 'S'

	[[A1]]
	test = 'St'

		[[[A2]]]
		test  = 'Stuff'			## inline comments are allowed
		test2 = 'Stuff2'
		test3 = 'a','b','c'		## lists are supported!
		test4 = ,					## empty list!

['B']
test = 'B'


WARNING:

configobj does not support multiline lists



Read

#### Read from file
from configobj import ConfigObj

cfg = ConfigObj( 'myfile.ini')		## read from file
#### Read from string
from configobj import ConfigObj
from io        import StringIO

string = (
	"[heading]\n"
	"key = 'value'"
	"otherkey = 'othervalue'"
)

fake_file = StringIO( fake_file )

ConfigObj( fake_file )



Write

from configobj import ConfigObj

cfg = ConfigObj('somefile.ini')

cfg['key']    = 123
cfg['newkey'] = 456

cfg.write()