Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing .ini files in python

As per the documentation (https://docs.python.org/3/library/configparser.html), I use the configparser for parsing the .ini files

Code :

import ConfigParser
config = ConfigParser.ConfigParser(allow_no_value=True)
config.read('D:\\test\\sample.ini')
print(config.sections())

Sample ini file1 : (Working)

[Group1]   
test_value1=0  
test_value2=5

This code is working and it loads the sample ini file1 successfully

but few following ini files are not parsed using the above code, Could someone help me on this, please

Sample ini file 2 : (Not Working)

[Group1]   
    test_value1=0  
    test_value2=5

Sample ini file 3 : (Not Working)

   [Group1]
     [[inner_group1]]
       test_value1=0 
       test_value2=5

Any help is appreciated.

Thanks,
Harry

like image 838
Harry Avatar asked May 29 '26 07:05

Harry


1 Answers

Most ini parsers (link), including ConfigParser, don't support hierarchy a.k.a nested structures. For that you will need to pick a different format. Try YAML or JSON.

like image 52
Back2Basics Avatar answered May 30 '26 20:05

Back2Basics



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!