0

My ini file:

[1]

[4]

[5]

[6]

[9]

[1]

[1]

My python Script

import configparser
config = configparser.ConfigParser()
config.readfp(open('inifile.ini'))
print config.sections()

>>> ['1', '4', '5', '6', '9'] #where are two more '1' ?

How to get all actual values ? Configparser must be used.

  • Configparser does not support multiple sections of the same name, see a workaround here: http://stackoverflow.com/q/9876059/1781026 – chrki Nov 19 '16 at 10:18

2 Answers2

1

In ConfigParser, sections are unique. It stores the sections in map(). Hence they should be unique.

As per the ConfigParser document:

The ConfigParser class implements a basic configuration file parser language which provides a structure similar to what you would find on Microsoft Windows INI files.

As per the INI Files wiki:

In some implementations, duplicate sections simply merge their properties together, as if they occurred contiguously. Others may abort, or ignore some aspect of the INI file.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

Your conf file should be defined with both section name and section value. ini file:

[database]
host = www.examle.com
port = 1234

And try to read value with get method:

import configparser
config = configparser.ConfigParser()
config.readfp(open('inifile.ini'))

host = config.get('database', 'host')
port = config.getint('database', 'port')
linpingta
  • 2,324
  • 2
  • 18
  • 36