使用 configparser 写入文件时定义 config.ini 条目的顺序?

Define order of config.ini entries when writing to file with configparser?

我正在使用 Python configparser 生成 config.ini 文件来存储我的脚本配置。配置是由代码生成的,但文件的重点是,有一种外部方式可以在以后的阶段更改以编程方式生成的配置。所以文件需要易于阅读,配置选项应该很容易找到。 configparser 中的部分是一种很好的方式来确保,但是在一个部分中,条目似乎是随机排序的。例如这段代码:

import configparser
config = configparser.ConfigParser()

config['DEFAULT'] = {
    'shuffle': 'True',
    'augment': 'True',
    # [... some other options ...] 
    'data_layer_type' : 'hdf5',     
    'shape_img' : '[1024, 1024, 4]',    
    'shape_label' : '[1024, 1024, 1]', 
    'shape_weights' : '[1024, 1024, 1]' 
}

with open('config.ini', 'w') as configfile:
    config.write(configfile)

生成一个 config.ini-文件,顺序为:

[DEFAULT]
shape_weights = [1024, 1024, 1]
# [... some of the other options ...] 
shuffle = True
augment = True
data_layer_type = hdf5
# [... some of the other options ...] 
shape_img = [1024, 1024, 4]
# [... some of the other options ...] 
shape_label = [1024, 1024, 1]

即这些条目既不按字母顺序排列,也不按任何其他可识别的顺序排列。但我想要订单,例如形状选项都在同一个地方,没有分发给用户浏览...

Here 据说 Python 3.1 中修复了无序行为,默认情况下使用有序字典,但我使用的是 Python 3.5.2 并获得无序条目。是否有我需要设置的标志或对 dict 进行排序的方法,以便它会产生(至少)按字母顺序排序的条目?

在使用 configparser 以编程方式生成 config.ini 时,是否可以定义条目的顺序? (Python 3.5)

configparser 似乎默认使用 OrderedDicts(自 Python 2.7 / 3.1 起),这使得 ConfigParser(dict_type=OrderedDict) 已过时。但是,默认情况下这不会对条目进行排序,仍然需要手动执行(至少在我的情况下)。

我找到了执行此操作的代码 here 并添加了排序默认值:

import configparser
from collections import OrderedDict

# [...] define your config sections and set values here

#Order the content of DEFAULT section alphabetically
config._defaults = OrderedDict(sorted(config._defaults.items(), key=lambda t: t[0]))

#Order the content of each section alphabetically
for section in config._sections:
    config._sections[section] = OrderedDict(sorted(config._sections[section].items(), key=lambda t: t[0]))

# Order all sections alphabetically
config._sections = OrderedDict(sorted(config._sections.items(), key=lambda t: t[0] ))

这里的问题不是 configparser 没有在内部使用 OrderedDicts,而是你正在制作一个无序文字并分配它。

注意这不是排序的:

>>> x = {
...     'shuffle': 'True',
...     'augment': 'True',
...     # [... some other options ...] 
...     'data_layer_type' : 'hdf5',     
...     'shape_img' : '[1024, 1024, 4]',    
...     'shape_label' : '[1024, 1024, 1]', 
...     'shape_weights' : '[1024, 1024, 1]' 
... }
>>> for k in x:
...     print(k)
... 
shuffle
augment
shape_img
shape_label
shape_weights
data_layer_type

(这在 python3.6 中作为实施细节进行了更改,作为 "small dicts" 优化的一部分(所有指令都已排序)——可能会作为 python3 的一部分进行标准化.7 由于方便)

此处的解决方法是确保您一直在分配 OrderedDict

config['DEFAULT'] = collections.OrderedDict((
    ('shuffle', 'True'),
    ('augment', 'True'),
    # [... some other options ...] 
    ('data_layer_type', 'hdf5'),     
    ('shape_img', '[1024, 1024, 4]'),    
    ('shape_label', '[1024, 1024, 1]'), 
    ('shape_weights', '[1024, 1024, 1]'), 
))