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

3
  • Your example code produces an ModuleNotFoundError. Commented Aug 21, 2019 at 16:47
  • Have you tried anything? Commented Aug 21, 2019 at 16:55
  • You might want to use TOML, there are also Python implementations Commented Aug 21, 2019 at 17:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the input, Is there a way to do using python for parsing ini file?
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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.