ConfigParser getting value from INI file with section and subsection as shown below

bana picture bana · May 3, 2013 · Viewed 11.9k times · Source

I have a following type of INI file

[section1][subsection1]
port=989
[section1][subsection2]
somethign=somethign 

I am using ConfigParser of Python to parse the INI file but I am not able to figure it out on how to get the data from the above kinda INI file.

the below code is for getting the value when INI file is like

[section1]
port=908
[section2]
ss=ss

config = ConfigParser.RawConfigParser()
config.read(INI_File)
mIp =  config.get('section1','port')

Please don't suggest me to change the INI file format :)

thank you

Answer

M456 picture M456 · May 3, 2013

It appears that ConfigParser ignores subsections. If you absolutely need subsections, you may want to try alternative parsers such as ConfigObj (PyPi, tutorial)

And here is an answer to an older question: https://stackoverflow.com/a/3008051/49412