4

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

bana
  • 397
  • 1
  • 5
  • 16

1 Answers1

5

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

Community
  • 1
  • 1
M456
  • 5,547
  • 2
  • 19
  • 14
  • I tried ConfigObj but unfortunately it only parses the INI files in the below format `[section] [[subsection]] blah=1` – bana May 03 '13 at 17:03
  • Thank you for sharing ConfigObj! It's exactly what I was looking for! – Vreality Mar 18 '17 at 20:57