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

拟墨画扇 提交于 2019-12-12 17:34:13

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/16350462/configparser-getting-value-from-ini-file-with-section-and-subsection-as-shown-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!