Hello, there is an .ini file, it has the following content:

`[MAIN] att=9 w_obnaruz=1` 

How to use a .json file with this content:

 `{ "w_obnaruz": { "ini section": "MAIN", "json name": ["main", "detect"], "val": "bool(old)" }, "att": { "ini section": "MAIN", "json name": ["main", "att"], "val": "old" } }` 

get the output .json file of such a plan:

 `{ "main": { "detect": True, "att": 9 } }` 

    1 answer 1

     In [1]: import ConfigParser In [2]: ini_string = """ ...: [MAIN] ...: att=9 ...: w_obnaruz=1 ...: """ In [3]: config = ConfigParser.RawConfigParser() In [4]: import io In [5]: config.readfp(io.BytesIO('\n'.join([line.strip() for line in ini_string.strip().splitlines()]))) In [6]: import json In [8]: params = json.loads(""" ...: { ...: "w_obnaruz": { ...: "ini section": "MAIN", ...: "json name": ["main", "detect"], ...: "val": "bool(old)" ...: }, ...: "att": { ...: "ini section": "MAIN", ...: "json name": ["main", "att"], ...: "val": "old" ...: } ...: } ...: """) In [13]: result = {} In [28]: for item, value in params.items(): if not value['json name'][0] in result: result[value['json name'][0]] = {} result[value['json name'][0]][value['json name'][1]] = config.getboolean(value['ini section'], item) if 'bool' in value['val'] else config.get(value['ini section'], item) ....: In [29]: json.dumps(result) Out[29]: '{"main": {"att": "9", "detect": true}}' 
    • Thank you for your answer, but it is not completely clear to me. You can comment. - XFixer
    • what exactly is not clear? - actionless
    • Here's what In [28] - XFixer
    • there, in fact, 4 lines. which one is incomprehensible? - actionless
    • one
      Result [category] [parameter] assign the value of this parameter from the config as a Boolean if there is a substring 'bool', otherwise assign the value of this parameter from the config as is - actionless