iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pythonconfigparser中默认值的设定方式
  • 525
分享到

pythonconfigparser中默认值的设定方式

2024-04-02 19:04:59 525人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录configparser中默认值的设定解决方案使用configparser的注意事项注意要点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’,并不影响配置的正确读写。 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: pythonconfigparser中默认值的设定方式

本文链接: https://www.lsjlt.com/news/138764.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作