1

Please advice how do we loop through subsection in Python - configparser.

[KUBENAMESPACE1]
  [MONITOR_CONFIG1]
  DEPLOYMENT_NAME = XXX
  MIN_REPLICAS = 1
  MAX_REPLICAS = 10
  [MONITOR_CONFIG2]
  DEPLOYMENT_NAME = XXX
  MIN_REPLICAS = 1
  MAX_REPLICAS = 10

[KUBENAMESPACE2]

2 Answers 2

1

From the documentation of configparser, it only supports one section level.

It would be much easier and cleaner to maintain this in a JSON file.

{
    "KUBENAMESPACE1": {
        "MONITOR_CONFIG1": {
            "DEPLOYMENT_NAME": "XXX",
            "MIN_REPLICAS": "1",
            "MAX_REPLICAS": "10"
        },
        "MONITOR_CONFIG2": {
            "DEPLOYMENT_NAME": "XXX",
            "MIN_REPLICAS": "1",
            "MAX_REPLICAS": "10"
        }
    },
    "KUBENAMESPACE2": {}
}
Sign up to request clarification or add additional context in comments.

Comments

0

From my working application:

import configparser

config = configparser.ConfigParser()
config.read("settings.ini")

for section in config.sections():
    print(f"[{section}]")
    for key, value in config.items(section):
        print(f"\tfor key {key} -> {value} (value)")

Links:

https://en.wikipedia.org/wiki/INI_file#Format

https://linuxhint.com/python-configparser-example/

Comments

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.