简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM