简体   繁体   English

ConfigParser从INI文件中获取带有节和小节的值,如下所示

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

I have a following type of INI file 我有以下类型的INI文件

[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. 我正在使用Python的ConfigParser解析INI文件,但无法弄清楚如何从上述INI文件中获取数据。

the below code is for getting the value when INI file is like 下面的代码用于在INI文件类似时获取值

[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 :) 请不要建议我更改INI文件格式:)

thank you 谢谢

It appears that ConfigParser ignores subsections. 似乎ConfigParser忽略了子节。 If you absolutely need subsections, you may want to try alternative parsers such as ConfigObj ( PyPi , tutorial ) 如果您绝对需要这些小节,则可以尝试使用其他解析器,例如ConfigObj( PyPitutorial

And here is an answer to an older question: https://stackoverflow.com/a/3008051/49412 这是一个老问题的答案: https : //stackoverflow.com/a/3008051/49412

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM