3

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

Harry
  • 3,072
  • 6
  • 43
  • 100

1 Answers1

5

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.

Back2Basics
  • 7,406
  • 2
  • 32
  • 45
  • Thanks for the input, Is there a way to do using python for parsing ini file? – Harry Aug 22 '19 at 03:50
  • Since these aren't standard INI files, I would rename them to something else and start building your own parser for it. It's a hierarchical INI file so you could call it .hini (building parsers is outside of the scope of this question though.) – Back2Basics Aug 23 '19 at 22:05