python configparser中默认值的设定方式

 更新时间:2022年02月10日 17:04:10   作者:Believer007  
这篇文章主要介绍了python configparser中默认值的设定方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

configparser中默认值的设定

在做某一个项目时,在读配置文件中,当出现配置文件中没有对应项目时,如果要设置默认值,以前的做法是如下的:

try:
    apple = config.get(section, 'apple')
except NoSectionError, NoOptionError:
    apple = None

但当存在很多配置时,这种写法太糟糕

幸好,在Configparser.get()函数中有一个vars()的参数,可以自定义;注:只能用ConfigParser.ConfigParser;rawconfigparser是不支持的

解决方案

1、定义函数:

class DefaultOption(dict):
    def __init__(self, config, section, **kv):
        self._config = config
        self._section = section
        dict.__init__(self, **kv)
    def items(self):
        _items = []
        for option in self:
            if not self._config.has_option(self._section, option):
                _items.append((option, self[option]))
            else:
                value_in_config = self._config.get(self._section, option)
                _items.append((option, value_in_config))
        return _items

2、使用

def read_config(section, location):
    config = configparser.ConfigParser()
    config.read(location)
    apple = config.get(section, 'apple',
                       vars=DefaultOption(config, section, apple=None))
    pear = config.get(section, 'pear',
                      vars=DefaultOption(config, section, pear=None))
    banana = config.get(section, 'banana',
                        vars=DefaultOption(config, section, banana=None))
    return apple, pear, banana

这样就很好解决了读取配置文件时没有option时自动取默认值,而不是用rasie的方式取默认值

此方案来之stackoverflow

使用configparser的注意事项

以这个非常简单的典型配置文件为例:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no

1、config parser 操作跟dict 类似,在数据存取方法基本一致

>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

2、默认配置项[DEFAULT]section 的默认参数会作用于其他Sections

3、数据类型

  • config parsers 不会猜测或自动分析识别config.ini参数的数据类型,都会按照字符串类型存储,如果需要读取为其他数据类型,需要自定义转换。
  • 特殊bool值:对于常见的布尔值’yes’/‘no’, ‘on’/‘off’, ‘true’/‘false’ 和 ‘1’/‘0’,提供了getboolean()方法。

4、获取参数值方法 get()

  • 使用get()方法获取每一参数项的配置值。
  • 如果一般Sections 中参数在[DEFAULT]中也有设置,则get()到位[DEFAULT]中的参数值。

5、参数分隔符可以使用‘=’或‘:’(默认)

6、可以使用‘#’或‘;’(默认)添加备注或说明 

[Simple Values]
key=value
spaces in keys=allowed
spaces in values=allowed as well
spaces around the delimiter = obviously
you can also use : to delimit keys from values
[All Values Are Strings]
values like this: 1000000
or this: 3.14159265359
are they treated as numbers? : no
integers, floats and booleans are held as: strings
can use the API to get converted values directly: true
[Multiline Values]
chorus: I'm a lumberjack, and I'm okay
    I sleep all night and I work all day
[No Values]
key_without_value
empty string value here =
[You can use comments]
# like this
; or this
# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.
    [Sections Can Be Indented]
        can_values_be_as_well = True
        does_that_mean_anything_special = False
        purpose = formatting for readability
        multiline_values = are
            handled just fine as
            long as they are indented
            deeper than the first line
            of a value
        # Did I mention we can indent comments, too?

7、写配置

常见做法:

config.write(open('example.ini', 'w'))

合理做法:

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

注意要点

1、ConfigParser 在get 时会自动过滤掉‘#’或‘;‘注释的行(内容);

  • 一般情况下我们手工会把配置中的暂时不需要的用‘#‘注释,问题在于,Configparser 在wirte的时候同file object行为一致,如果将注释’#‘的配置经过get后,再wirte到conf,那么’#‘的配置就会丢失。
  • 那么就需要一个策略或规则,配置需不需要手工编辑 ?还是建立复杂的对原生文本的处理的东西,我建议是管住手,避免将一些重要的配置爆露给用户编辑,切记行内注释和Section内注释。
  • 有一个相对简单的方法是:
  • 对单独在一行的代码,你可以在读入前把"#", ";"换成其他字符如’@’,或‘^’(在其bat等其他语言中用的注释符易于理解),使用allow_no_value选项,这样注释会被当成配置保存下来,处理后你再把“#”, ";"换回来。

2、在ConfigParser write之后,配置文本如果有大写字母’PRODUCT’会变为小写字母’product’,并不影响配置的正确读写。 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 解决Pytorch中Batch Normalization layer踩过的坑

    解决Pytorch中Batch Normalization layer踩过的坑

    这篇文章主要介绍了解决Pytorch中Batch Normalization layer踩过的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • 详解python 中in 的 用法

    详解python 中in 的 用法

    in在Python中是操作符,具体来说是成员操作符。这篇文章主要介绍了python 中in 的 用法,需要的朋友可以参考下
    2019-12-12
  • Linux中Python 环境软件包安装步骤

    Linux中Python 环境软件包安装步骤

    本文给大家分享的是在Linux系统中Python环境的安装步骤,以及常用的软件的安装升级,非常的实用,有需要的小伙伴可以参考下
    2016-03-03
  • 聊聊Python代码中if __name__ == ‘__main__‘的作用是什么

    聊聊Python代码中if __name__ == ‘__main__‘的作用是什么

    一个python文件通常有两种使用方法,第一是作为脚本直接执行,第二是 import 到其他的python脚本中被调用执行,这篇文章主要给大家介绍了关于Python代码中if __name__ == ‘__main__‘的作用是什么的相关资料,需要的朋友可以参考下
    2022-03-03
  • Python3.5实现的三级菜单功能示例

    Python3.5实现的三级菜单功能示例

    这篇文章主要介绍了Python3.5实现的三级菜单功能,涉及Python针对json格式数据的读取、遍历、查找、判断等相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • python同时遍历两个list用法说明

    python同时遍历两个list用法说明

    这篇文章主要介绍了python同时遍历两个list用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • python实现的DES加密算法和3DES加密算法实例

    python实现的DES加密算法和3DES加密算法实例

    这篇文章主要介绍了python实现的DES加密算法和3DES加密算法,以实例形式较为详细的分析了DES加密算法和3DES加密算法的原理与实现技巧,需要的朋友可以参考下
    2015-06-06
  • Python 数据结构之堆栈实例代码

    Python 数据结构之堆栈实例代码

    这篇文章主要介绍了Python 数据结构之堆栈实例代码的相关资料,需要的朋友可以参考下
    2017-01-01
  • 浅析python中的二元嵌套列表分组

    浅析python中的二元嵌套列表分组

    这篇文章主要来和大家一起讨论一下Python中的二元嵌套列表,并将每个嵌套列表元素相对于其其他索引元素进行分组,感兴趣的小伙伴可以学习一下
    2023-09-09
  • python嵌套异常的两种处理器

    python嵌套异常的两种处理器

    在Python中,异常也可以嵌套,本文主要介绍了python嵌套异常的两种处理器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-01-01

最新评论