5. Management of items of section

5.1. Set or add new an option

section1 = config.add_section('section1', text_after=' # this is comment')

section1['option1'] = 'value1'
section1['option_without_value'] = None
section1.set_option('option2', 200)

or you can adding option with custom separator or position:

section1.set_option('option1', 100, sep=' == ', pos=0)
section1.set_option('option_without_value', pos=0)

Result:

[section1] # this is comment
option_without_value
option1 == 100
option2 = 200

Also, you can set option to non-section area:

non_section = config[None]
non_section['global_parameter'] = 1

Result:

global_parameter = 1
[section1] # this is comment
option_without_value
option1 == 100
option2 = 200

You can set option with custom indentation:

section1['    option1'] = 100
section1.set_option('    option1', 100)

section1['    option_without_value'] = None
section1.set_option('    option_without_value')

Result:

global_parameter = 1
[section1] # this is comment
    option_without_value
    option1 = 100
option2 = 200

5.2. Get values of option

You can get an option value in three different ways:

value = section1['option1']
value = section1.get_value('option1')
value = section1.get_option('option1')['value']

Note

Also you can get value by index at section. See the chapter next.

5.3. Remove an option

del section1['option2']

or

section1.remove_option('option2')

Note

Also you can delete an option by index. See the chapter next.

5.4. Add a new item

Instead of using set_option() method, you can use the low-level method add_item() to add empty lines, comments to the section:

# append new option:
section1.add_item({'key': '    option2', 'sep': ' = ', 'value': 2})
# insert new option
section1.add_item({'key': '    option0', 'sep': ' = ', 'value': 0}, pos=0)
# insert empty line
section1.add_item('', pos=0)
# append empty line
section1.add_item('')
# insert comment
section1.add_item('    # This is comment for option1', 3)

Result:

global_parameter = 1
[section1] # this is comment

    option0 = 0
    option_without_value
    # This is comment for option1
    option1 = 100
    option2 = 2

5.5. Get an item by index

comment = section1[3]
option1_value = section1[4]['value']
last_item = section1[len(section1) - 1]

5.6. Remove an item by index

del section1[0]
del section1[5]
del section1[4]

Result:

global_parameter = 1
[section1] # this is comment
    option0 = 0
    option_without_value
    # This is comment for option1
    option1 = 100