Examples of reading/writing sorts of configuration files.
Feel free to get some inpiration, those are far from perfect and can always be improved upon.
PS: I'm a big fan of pathlib therefore I use it here ;-)
- python3
pip install -r requirements.txt or python -m pip install -r requirements.txt to install required modules
Anything goes ;-)
none
from json import loads, dumps
# loads to convert text to data
# dumps to convert data to text
PS: other functions exist ...
allows to use the dots to access fields instead of brackets and quotes
same but more python3-ish
not only you have fields here but also IDEs will validate your use of valid fields and auto-complete and loading validates the expected structure
TBD use the dotty module to allow single key to travel multiple levels deep into dicts
Mostly for simple structures
from yaml import load, dump
Only if you have no choice, right ?
from configparser import ConfigParser
config = ConfigParser()
config.read("/path/to/file.cfg")
with open("/path/to/file.cfg", "w") as f:
config.write(f)
Who on earth would do that ?
- add example with datetime serialization/deserialisation
- add dumps(d, default=str) example