简体   繁体   中英

Python - ConfigParser - .ini file too large?

This is my first question on stackoverflow and I have only been involved with python for a short time, but I have a problem with reading an .ini file and I cannot find the answer anywhere.

This is a small piece of my current code:

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

The script runs, but it does not print the names of the sections in my .ini file. I have waited for more than 15 min, but I still receive nothing. Could it be that the file is too large? It is 32.628 KB and I have to do the same thing multiple times so I was hoping I could automate it.

I hope someone can help me out

I'll assume you want to parse a normal INI file, this should be the source code for it (which your file probably does not follow, so you might consider following this format):

[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

Your code should work - with a small adjustment

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))

If you have a complex INI file (with sub sections) - then ConfigParser won't be able to help you here, and you'll need another parser for that, check this: ConfigParser getting value from INI file with section and subsection as shown below

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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