Paul
Paul

Reputation: 85

How to find value based on key without knowing section name

Is there a way to get the value of A for SEC-1 below if I only know the value for B (and don't now what section I will find that value for B in)?

So all I know is B=XX, and I want to get the A value of 101.

[SEC-1]
A=101
B=XX

[SEC-2]
A=102
B=YY

Upvotes: 0

Views: 31

Answers (1)

Rakesh
Rakesh

Reputation: 82765

This might help.

import configparser

config = configparser.ConfigParser()
config.read(filename)
for ses in config.sections():
    if config[ses].get("B") == "XX":    #Check if var B has XX
        print(config[ses].get("A"))

Upvotes: 1

Related Questions