使用configparser写入文件时定义config.ini条目的顺序? [英] Define order of config.ini entries when writing to file with configparser?

查看:118
本文介绍了使用configparser写入文件时定义config.ini条目的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python configparser 生成 config.ini 文件来存储我的脚本配置。配置是由代码生成的,但是文件的目的是要在以后的阶段使用外部方式更改以编程方式生成的配置。因此,文件必须易于阅读,并且配置选项应易于查找。 configparser中的各节是一种很好的方法,可确保在一个节中,条目似乎是随机排列的。例如以下代码:

I'm using the Python configparser to generate config.ini files to store my scripts configuration. The config is generated by code, but the point of the file is, to have an external way to change the programmatically generated config at later stages. So the file needs to be well readable and config options should be easy to find. The sections in configparser are a nice way to ensure that, however within a section, the entries seem to be ordered randomly. For example this code:

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 -文件的顺序如下:

generates a config.ini-File with the order:

[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]

即这些条目既不按字母顺序排列,也不按任何其他可识别的顺序排列。但是我想要点菜,例如形状选项全部位于同一位置,并且不分发给用户浏览...

i.e. the entries are neither in alphabetic nor in any other recognizable order. But I want order, e.g. the shape options all in the same place and not distributed for the user to browse...

此处指出,无序行为已在Python 3.1中修复,默认情况下使用有序字典,但我正在使用Python 3.5.2并获取无序条目。是否需要设置标志或对dict排序的方法,以便它会导致(至少)按字母顺序排序的条目?

Here it is stated that the unordered behavior was fixed in Python 3.1 to use ordered dicts by default, but I am using Python 3.5.2 and get unordered entries. Is there a flag I need to set or a way to sort the dict so that it will result in (at least) alphabetically sorted entries?

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

Is there a way to define order of the entries when programmatically generating the config.ini with the configparser? (Python 3.5)

推荐答案

这里的问题不是 configparser 没有使用 OrderedDict 在内部,这是因为您正在制作无序文字并将其分配。

The issue here is not that configparser isn't using OrderedDicts internally, it's that you're making an unordered literal and assigning that.

请注意,这是如何不进行排序的:

Notice how this is not ordered:

>>> 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中的实现细节,作为小命令的一部分优化(所有字典都已排序)-由于方便,可能已作为python3.7的一部分进行了标准化)

(This changes as an implementation detail in python3.6 as part of a "small dicts" optimization (all dicts become ordered) -- likely to be standardized as part of python3.7 due to convenience)

此处的解决方法是确保您分配了 OrderedDict 贯穿整个过程:

The fix here is to make sure you're assigning OrderedDicts all the way through:

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]'), 
))

这篇关于使用configparser写入文件时定义config.ini条目的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆