Convert ConfigParser.items('') to dictionary

后端 未结 9 1094
再見小時候
再見小時候 2020-12-12 18:44

How can I convert the result of a ConfigParser.items(\'section\') to a dictionary to format a string like here:

import ConfigParser

config = ConfigParser.Co         


        
9条回答
  •  猫巷女王i
    2020-12-12 19:42

    Here is another approach using Python 3.7 with configparser and ast.literal_eval:

    game.ini

    [assets]
    tileset = {0:(32, 446, 48, 48), 
               1:(96, 446, 16, 48)}
    

    game.py

    import configparser
    from ast import literal_eval
    
    config = configparser.ConfigParser()
    config.read('game.ini')
    
    # convert a string to dict
    tileset = literal_eval(config['assets']['tileset'])
    
    print('tileset:', tileset)
    print('type(tileset):', type(tileset))
    

    output

    tileset: {0: (32, 446, 48, 48), 1: (96, 446, 16, 48)}
    type(tileset): 
    

提交回复
热议问题