python configparser get_python ConfigParser模块get方法简介

ConfigParser模块get官方文档解释如下:

The ConfigParser class extends some methods of the RawConfigParser interface, adding some optional arguments.

ConfigParser.get(section, option[, raw[, vars]])

Get an option value for the named section. If vars is provided, it must be a dictionary. The option is looked up in vars (if provided), section, and in defaults in that order.

All the '%' interpolations are expanded in the return values, unless the raw argument is true. Values for interpolation keys are looked up in the same manner as the option.

ConfigParser.items(section[, raw[, vars]])

Return a list of (name, value) pairs for each option in the given section. Optional arguments have the same meaning as for the get() method.

New in version 2.3.

简单来说就是:获取命名部分的选项值

ConfigParser.get(section,option [,raw [,vars]])

section 配置名

option 选项名

raw bool类型 可选参数,默认为False

vars dict类型 可选参数

如果提供了vars 那么获取配置选项值得规则如下

先在vars中寻找,如果找到就使用vars中的值

如果找不到 就是用默认值

前提是raw的值是False

以下是测试代码

文件test.conf内容如下

[Section1]

foo=%(bar)s is %(baz)s!

baz=fun

bar=Python

测试代码:

#!/usr/bin/env python#-*- coding:utf-8 -*-

importConfigParserimportstring, os

cf=ConfigParser.ConfigParser()

cf.read("test.conf")

res= cf.get('Section1', 'foo')print "默认情况下, raw=False, 此时输出 %s" %res

res= cf.get('Section1', 'foo', raw=False)print "raw=False, 无参数vars 此时等同于默认输出:%s" %res

res= cf.get('Section1', 'foo', raw=True)print "raw=True, 无参数vars 此时等输出未被匹配原字符:%s" %res

res= cf.get('Section1', 'foo', raw=False, vars={'bar': 'Documentation','baz': 'evil'})print "raw=False, vars存在 此时使用vars中的值进行匹配:%s" %res

res= cf.get('Section1', 'foo', raw=True, vars={'bar': 'Documentation', 'baz':'sdsd'})print "raw=True, vars存在 此时vars不生效,输出未被匹配原字符:%s" %res

res= cf.get('Section1', 'foo', raw=False, vars={'bar': 'Documentation'})print "raw=True, vars存在,但只包含一个值, 此时另一个值取默认匹配值,输出未:%s" % res

输出如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
configparser模块是一个用于读取和写入配置文件的Python标准模块。配置文件通常是用于存储应用程序的设置和配置信息的文件。下面是使用configparser模块的基本步骤: 1. 导入configparser模块: ```python import configparser ``` 2. 创建一个ConfigParser对象: ```python config = configparser.ConfigParser() ``` 3. 读取配置文件: ```python config.read('config.ini') ``` 4. 获取配置项的值: ```python value = config.get('section', 'option') ``` 其中,section是配置文件中的一个段落,option是段落中的一个选项。例如,在以下配置文件中: ``` [database] host = localhost port = 3306 user = root password = test123 database = testdb ``` 要获取host的值,可以使用以下代码: ```python host = config.get('database', 'host') print(host) # 输出:localhost ``` 5. 修改配置项的值: ```python config.set('section', 'option', 'new_value') ``` 例如,要将host修改为127.0.0.1,可以使用以下代码: ```python config.set('database', 'host', '127.0.0.1') ``` 6. 写入配置文件: ```python with open('config.ini', 'w') as f: config.write(f) ``` 完整示例: ```python import configparser config = configparser.ConfigParser() # 读取配置文件 config.read('config.ini') # 获取配置项的值 host = config.get('database', 'host') port = config.getint('database', 'port') user = config.get('database', 'user') password = config.get('database', 'password') database = config.get('database', 'database') print('host:', host) print('port:', port) print('user:', user) print('password:', password) print('database:', database) # 修改配置项的值 config.set('database', 'host', '127.0.0.1') # 写入配置文件 with open('config.ini', 'w') as f: config.write(f) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值