如何在python中读取json嵌入的ini配置文件

如何在python中读取json嵌入的ini配置文件,python,json,runtime-error,config,python-3.4,Python,Json,Runtime Error,Config,Python 3.4,我使用的是Python3.4,我在以格式化方式读取json格式的配置文件时遇到问题(即,不在同一行中) 我的配置文件“Example.config”如下所示 [ABC] outdoor = {'GAME':['cricket', 'football'], 'INFO':[ {'Players':'eleven', 'Fun':'Unlimite

我使用的是Python3.4,我在以格式化方式读取json格式的配置文件时遇到问题(即,不在同一行中) 我的配置文件“Example.config”如下所示

[ABC]

outdoor = {'GAME':['cricket',
                    'football'],
           'INFO':[
                      {'Players':'eleven',
                       'Fun':'Unlimited'},
                      {'Players':'something',
                       'Fun':'Much_more'}
                     ]
         }

[XYZ]

INDOOR = {'GAME':['Carom'],
           'INFO':[
                        {'Players':'2','Fun':'Less'},
                        {'Players':'4','Fun':'More'}
                      ]
          }

INDOOR1 = {'GAME':['TABLE-TENNIS',
                    'BASKETBALL',
                    'ANYTHING'],
          'INFO':[
                    {'Players':'TWO','Fun':'MORE'},
                    {'Players':'MORE','Fun':'MUCHMORE'}
                     ]
         }
麦可德

config_parser=ConfigParser()
    config_parser.read('Example.config')
    for each_section in config_parser.sections():
        for option in config_parser.options(each_section):
            option=ast.literal_eval(option)
            try:
                games=option['GAME']
                info=option['INFO']
            except:
                print('PLease format config same as qa_config.config file.Please Pay special attention to casing')
                raise

            for game in games:
                for eachInfo in info:
                    _INFO=eachInfo['Players']
                    _FUN=eachInfo['Fun']
                    print (each_section,':\n')
                    print ('\t',game,':\n')
                    print ('\t',_INFO,':\n')
                    print ('\t',_FUN,':\n')
错误:

option=ast.literal_eval(option)
  File "C:\Python34\lib\ast.py", line 84, in literal_eval
    return _convert(node_or_string)
  File "C:\Python34\lib\ast.py", line 83, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x02E61910>
所以我想这是因为我在配置文件中使用了制表符和空格。但我需要美化配置文件,因为用户将更新它


我还尝试了
asteval
json.load
。正如您所发现的,这不是有效的INI语法。如果你真的需要那种深度结构化的数据,考虑只使用YAML来配置文件,它看起来是这样的:

ABC:
  outdoor:
    GAME:
       - cricket
       - football
    INFO:
      - Players: eleven
        Fun: unlimited
      - Players: something
        Fun: much more
>>> import yaml
>>> with open('myconfig.yml') as fd:
...   config = yaml.load(fd)
然后像这样解析:

ABC:
  outdoor:
    GAME:
       - cricket
       - football
    INFO:
      - Players: eleven
        Fun: unlimited
      - Players: something
        Fun: much more
>>> import yaml
>>> with open('myconfig.yml') as fd:
...   config = yaml.load(fd)
如您所见,它直接解析为Python数据结构:

>>> import pprint
>>> pprint.pprint(config)
{'ABC': {'outdoor': {'GAME': ['cricket', 'football'],
                     'INFO': [{'Fun': 'unlimited', 'Players': 'eleven'},
                              {'Fun': 'much more', 'Players': 'something'}]}}}

我尝试了你的代码和示例(承认在2.7中)并解决了这个问题 是因为您在错误的事情上使用了
ast.literal\u eval()

您正在
选项上使用它,该选项是
ini文件(在您的示例中为单词outdoor、INDOOR1和INDOOR1)。
您希望在该选项的右侧值使用它

将下面缺少的第2行添加到脚本中的相应循环中:

    for option in config_parser.options(each_section):
        option = config_parser.get(each_section, option)
        option = ast.literal_eval(option)
您的多行ini文件已完全解析