繁体   English   中英

Python-ConfigParser-.ini文件太大?

[英]Python - ConfigParser - .ini file too large?

这是我关于stackoverflow的第一个问题,我只参与了很短一段时间的python,但是我在读取.ini文件时遇到问题,无法在任何地方找到答案。

这是我当前代码的一小部分:

import configparser 
config = configparser.ConfigParser()
config.sections()
config.read("filename.ini")
print(config.sections)

该脚本会运行,但是不会在我的.ini文件中打印各节的名称。 我已经等待了15分钟以上,但仍然什么也没收到。 可能是文件太大了吗? 它是32.628 KB,我必须多次执行相同的操作,因此我希望可以实现自动化。

我希望有人可以帮助我

我假设您想解析一个普通的INI文件,这应该是它的源代码(您的文件可能不遵循该源代码,因此您可以考虑采用这种格式):

[section_1]
section_1_1: test0
section_1_2: test1
section_1_3: test3

[section_2]
section_2_1: test0
section_2_2: test1
section_2_3: test3

您的代码应该可以工作-稍作调整

from configparser import ConfigParser 
config = ConfigParser()
config.sections()
config.read("conf.ini")
print(config.sections())
for section in config.sections():
    for option in config.options(section):
        print("Option: {} in section: {}".format(option, section))

如果您有一个复杂的INI文件(包含子节),则ConfigParser将无法在这里为您提供帮助,为此您将需要另一个解析器,请检查此内容: ConfigParser从INI文件中获取带有节和子节的值,如图所示下面

暂无
暂无

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

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