ConfigParser从INI文件中获取带有节和小节的值,如下所示 [英] ConfigParser getting value from INI file with section and subsection as shown below
问题描述
我有以下类型的INI文件
I have a following type of INI file
[section1][subsection1]
port=989
[section1][subsection2]
somethign=somethign
我正在使用Python的ConfigParser解析INI文件,但无法弄清楚如何从上述INI文件中获取数据.
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.
下面的代码用于在INI文件类似于
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')
请不要建议我更改INI文件格式:)
Please don't suggest me to change the INI file format :)
谢谢
推荐答案
看来ConfigParser会忽略子节.如果您绝对需要这些小节,则可以尝试使用其他解析器,例如ConfigObj( PyPi , 教程)
It appears that ConfigParser ignores subsections. If you absolutely need subsections, you may want to try alternative parsers such as ConfigObj (PyPi, tutorial)
这是一个较旧问题的答案: https://stackoverflow.com/a/3008051/49412
And here is an answer to an older question: https://stackoverflow.com/a/3008051/49412
这篇关于ConfigParser从INI文件中获取带有节和小节的值,如下所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!