Python ConfigParser 注意事项

Python 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’,并不影响配置的正确读写。

待续。。。。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值