Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a configuration file in Python (storing/reading nested data with ConfigParser)

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

like image 997
Homunculus Reticulli Avatar asked Feb 15 '12 11:02

Homunculus Reticulli


People also ask

How read config file in Python?

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.

How do I use Configparser in Python?

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.

How do I read a Yaml config file in Python?

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.

Which module is used to read .ini config file in Python?

The configparser module can be used to read configuration files.


2 Answers

No, configparser doesn't support nesting. You could look at configObj instead. It's mature and quite widely used.

like image 89
seb Avatar answered Sep 21 '22 00:09

seb


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.

like image 30
Nilesh Avatar answered Sep 17 '22 00:09

Nilesh