I am writing a list processing script that needs to read configuration data about each item in the list. The configuration data is best represented as a nested tree.
I would normally have used YAML for storing the data - but I think that using ConfigParser would be a more Pythonic approach - and make the script more 'transparent' to other Python coders - since a surprising number of people are not familiar with the YAML format.
I have had a very quick look at the configParser documentation, but I have not been able to ascertain whether it can deal with nested data.
My configuration data will have the following structure:
<markers>
<marker>
<date></date>
<value></value>
</marker>
</markers>
<items>
<item>
<start></start>
<end></end>
<mcc>
<chg>
<date></date>
<ival></ival>
<fval></fval>
</chg>
</mcc>
</item>
</items>
Can I use ConfigParser to read/(write ?) this kind of nested data in a config file? (I'm more interested in being able to read than writing the config file. I don't mind manually writing the config file if necessary).
Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.
Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.
Reading a key from YAML config file We can read the data using yaml. load() method which takes file pointer and Loader as parameters. FullLoader handles the conversion from YAML scalar values to the Python dictionary. The index [0] is used to select the tag we want to read.
The configparser module can be used to read configuration files.
No, configparser
doesn't support nesting. You could look at configObj instead. It's mature and quite widely used.
As per your xml data you need section and subsection. So you can use the ConfigParser
but you have to give sub section with some meaning like
[markers]
[markers.marker]
date=''
value=''
[items]
[items.item]
start=''
end=''
[items.item.mcc]
[items.item.mcc.chg]
date=''
ival=''
fval=''
Then you have to override the getsection
function to get the nested data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With