Currently, I am using configparser to make a dictionary of Person-class objects from a .ini file, which looks like this (in the real dictionary, there are many more people):
[Pers1]
name = Alice
sex = female
[Pers2]
name = Bob
sex = male
This parsed dictionary is called people
, so people['Pers1'].name
is the name
attribute of Pers1
, "Alice"
. people['Pers2'].sex
is "male"
, and so on.
Now, let's say I want Alice to have another attribute, called tags
. tags
would be a dictionary, with a structure like this:
people['Pers1'].tags = {
'Employed':
{COMPANY: 'Macrosoft',
EMPLOYEE_SINCE: '1/5/2005'
},
'LicensedPilot':
{LICENSE_ISSUED: '6/7/2007',
EXPIRATION_DATE: '12/11/2017'
}
}
tags
is a dictionary that may be empty, or may contain more dictionaries (though only nested one-deep, so no dictionaries-of-dictionaries-of-dictionaries... in tags
).
Is there a way to set up configparser to read a .ini and create these dictionaries?
INI by itself is a flat format. If you want to store nested data in it, you will need to handle that.
For example, qualified property names:
[Pers1]
name = Alice
sex = female
tags.COMPANY = Macrosoft
tags.EMPLOYEE_SINCE = 1/5/2005
Note, these will still be returned as a 'flat' options
list, e.g. the first tag will have a key of "tags.COMPANY"
. You would need split the option names on .
and handle the results yourself.
Another approach would be to use a format that is meant to store nested data and be translated directly to lists and dictionaries, such as YAML.