iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python configparser中的默认值怎么设定
  • 858
分享到

python configparser中的默认值怎么设定

2023-06-29 03:06:11 858人浏览 独家记忆

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

摘要

本文小编为大家详细介绍“python configparser中的默认值怎么设定”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python configparser中的默认值怎么设定”文章能帮助大家解决疑惑,下面跟着小

本文小编为大家详细介绍“python configparser中的默认值怎么设定”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python configparser中的默认值怎么设定”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

configparser中默认值的设定

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

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

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

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

解决方案

定义函数:

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

使用

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 = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = hg[topsecret.server.com]Port = 50022ForwardX11 = no

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 configTrue>>> 'bytebong.com' in configFalse>>> 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)...usercompressionlevelserveraliveintervalcompressionforwardx11>>> config['bitbucket.org']['ForwardX11']'yes'

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

数据类型

  • config parsers 不会猜测或自动分析识别config.ini参数的数据类型,都会按照字符串类型存储,如果需要读取为其他数据类型,需要自定义转换。

  • 特殊bool值:对于常见的布尔值’yes’/‘no’, ‘on’/‘off’, ‘true’/‘false’ 和 ‘1’/‘0’,提供了getboolean()方法。

获取参数值方法 get()

  • 使用get()方法获取每一参数项的配置值。

  • 如果一般Sections 中参数在[DEFAULT]中也有设置,则get()到位[DEFAULT]中的参数值。

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

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

[Simple Values]key=valuespaces in keys=allowedspaces in values=allowed as wellspaces around the delimiter = obviouslyyou can also use : to delimit keys from values[All Values Are Strings]values like this: 1000000or this: 3.14159265359are they treated as numbers? : nointegers, floats and booleans are held as: stringscan 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_valueempty 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?

写配置

常见做法:

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

合理做法:

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

注意要点

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

  • 一般情况下我们手工会把配置中的暂时不需要的用‘#‘注释,问题在于,Configparser 在wirte的时候同file object行为一致,如果将注释’#‘的配置经过get后,再wirte到conf,那么’#‘的配置就会丢失。

  • 那么就需要一个策略或规则,配置需不需要手工编辑 ?还是建立复杂的对原生文本的处理的东西,我建议是管住手,避免将一些重要的配置爆露给用户编辑,切记行内注释和Section内注释。

  • 有一个相对简单的方法是:

  • 对单独在一行的代码,你可以在读入前把"#", ";"换成其他字符如’@’,或‘^’(在其bat等其他语言中用的注释符易于理解),使用allow_no_value选项,这样注释会被当成配置保存下来,处理后你再把“#”, ";"换回来。

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

读到这里,这篇“python configparser中的默认值怎么设定”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网Python频道。

--结束END--

本文标题: python configparser中的默认值怎么设定

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

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

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

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

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

  • 微信公众号

  • 商务合作