Python configobj: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 6: Line 6:
}}
}}


= Documentation =
<blockquote>
{| class="wikitable"
|-
| github || https://github.com/DiffSK/configobj
|-
|}
</blockquote><!-- Documentation -->


= Format =
= Format =

Revision as of 16:17, 2 October 2022

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

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()