加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python configparser模块

发布时间:2020-12-17 00:02:24 所属栏目:Python 来源:网络整理
导读:h1 id="简介"简介 ConfigParser 模块在python3中修改为 configparser .这个模块定义了一个ConfigParser类,该类的作用是使用配置文件生效,配置文件的格式和windows的INI文件的格式相同 该模块的 就是使用模块中的 RawConfigParser() 、 ConfigParser() 、?

<h1 id="简介">简介

ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类的作用是使用配置文件生效,配置文件的格式和windows的INI文件的格式相同

该模块的就是使用模块中的RawConfigParser()ConfigParser()、?SafeConfigParser()这三个方法,创建一个对象使用对象的方法对指定的配置文件做操作。

配置文件有不同的片段组成和Linux中repo文件中的格式类似:

格式:

=[DEFAULT] <span style="color: #008000;">#<span style="color: #008000;">设置默认的变量值,初始化[My Section] foodir: %(dir)s/whatever dir=frob <span class="hljs-keyword">long: <span class="hljs-keyword">this <span class="hljs-keyword">value continues <span class="hljs-keyword">in the next line

%(dir)s?会被frob代替。默认值会以字典的形式传递给ConfigParser的构造器。section一般存放的哦内置目录下,如果切换到其他的目录需啊哟指定存放位置。

方法:

下面这三种方式使用时,切记

在调用这三个函数时,切记这三个函数会将调用optionxform(),在传递键值对数据时,会将全部转化为

defaults : 如果指定默认值,则使用默认值的键值对
dict_type:使用新的section的键值对
allow_no_value :默认是False,如果是True,表示可以接收空值(None)
<span style="color: #0000ff;">return:对象

不支持可变参数,在section中不能存在%()s

ConfigParser.ConfigParser([defaults[,allow_no_value]]])

在default中必须出现%()s

ConfigParser.SafeConfigParser([defaults[,allow_no_value]]])

更加智能化,在section中是否存在%()s会自动判断

传递参数使用函数optionxform(),foo %(bar)s 和 foo %(BAR)s是相同的,optionxform()会将大写字母全部转换为小写。

异常

对象的操作可以分为两大类,一种是对配置文件的操作,另一种是对读取后数据流的操作。

读取配置文件:

描述
方法 。 写入配置文件:

描述
方法 </tr>
</table>

<h4 id="对内存中数据流的操作"><span style="font-family: 楷体; font-size: x-large;">对内存中数据流的操作

增加配置文件中的值:

方法 删除配置文件中的值:

描述
方法 修改配置文件中的值:

方法
查找配置文件中的值:

描述
描述
方法 config =<span style="color: #000000;"> ConfigParser.ConfigParser()
config.readfp(open(
<span style="color: #800000;">'<span style="color: #800000;">defaults.cfg<span style="color: #800000;">'<span style="color: #000000;">))
config.read([<span style="color: #800000;">'<span style="color: #800000;">site.cfg<span style="color: #800000;">',os.path.expanduser(<span style="color: #800000;">'<span style="color: #800000;">~/.myapp.cfg<span style="color: #800000;">')])

cfgparser == str

SafeConfigParser中包含ConfigParser相同的方法,还有一部分增加的方法

描述
方法 所有的“%”插值都在返回值中展开,除非原始的参数是真的。内插键的值与选项相同

set(section,value)?如果给定的部分存在,将给定的选项设置为指定的值;否则提高NoSectionError。值必须是字符串(str或unicode);如果没有,则会出现类型错误

?

config =<span style="color: #000000;"> ConfigParser.RawConfigParser()

<span style="color: #008000;">#<span style="color: #008000;"> When adding sections or items,add them in the reverse order of<span style="color: #008000;">

<span style="color: #008000;"> how you want them to be displayed in the actual file.<span style="color: #008000;">

<span style="color: #008000;"> In addition,please note that using RawConfigParser's and the raw<span style="color: #008000;">

<span style="color: #008000;"> mode of ConfigParser's respective set functions,you can assign<span style="color: #008000;">

<span style="color: #008000;"> non-string values to keys internally,but will receive an error<span style="color: #008000;">

<span style="color: #008000;"> when attempting to write to a file or when you get it in non-raw<span style="color: #008000;">

<span style="color: #008000;"> mode. SafeConfigParser does not allow such assignments to take place.

config.add_section(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">'<span style="color: #000000;">)
config.set(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">an_int<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">15<span style="color: #800000;">'<span style="color: #000000;">)
config.set(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">a_bool<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">true<span style="color: #800000;">'<span style="color: #000000;">)
config.set(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">a_float<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">3.1415<span style="color: #800000;">'<span style="color: #000000;">)
config.set(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">baz<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">fun<span style="color: #800000;">'<span style="color: #000000;">)
config.set(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">bar<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">Python<span style="color: #800000;">'<span style="color: #000000;">)
config.set(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">foo<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">%(bar)s is %(baz)s!<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #008000;">#<span style="color: #008000;"> Writing our configuration file to 'example.cfg'
with open(<span style="color: #800000;">'<span style="color: #800000;">example.cfg<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">wb<span style="color: #800000;">'<span style="color: #000000;">) as configfile:
config.write(configfile)

config =<span style="color: #000000;"> ConfigParser.RawConfigParser()
config.read(
<span style="color: #800000;">'<span style="color: #800000;">example.cfg<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #008000;">#<span style="color: #008000;"> getfloat() raises an exception if the value is not a float<span style="color: #008000;">

<span style="color: #008000;"> getint() and getboolean() also do this for their respective types

a_float = config.getfloat(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">a_float<span style="color: #800000;">'<span style="color: #000000;">)
an_int = config.getint(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">an_int<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">print a_float +<span style="color: #000000;"> an_int

<span style="color: #008000;">#<span style="color: #008000;"> Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.<span style="color: #008000;">

<span style="color: #008000;"> This is because we are using a RawConfigParser().

<span style="color: #0000ff;">if config.getboolean(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">a_bool<span style="color: #800000;">'<span style="color: #000000;">):
<span style="color: #0000ff;">print config.get(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">foo<span style="color: #800000;">')

config =<span style="color: #000000;"> ConfigParser.ConfigParser()
config.read(
<span style="color: #800000;">'<span style="color: #800000;">example.cfg<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #008000;">#<span style="color: #008000;"> Set the third,optional argument of get to 1 if you wish to use raw mode.
<span style="color: #0000ff;">print config.get(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',0) <span style="color: #008000;">#<span style="color: #008000;"> -> "Python is fun!"
<span style="color: #0000ff;">print config.get(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',1) <span style="color: #008000;">#<span style="color: #008000;"> -> "%(bar)s is %(baz)s!"

<span style="color: #008000;">#<span style="color: #008000;"> The optional fourth argument is a dict with members that will take<span style="color: #008000;">

<span style="color: #008000;"> precedence in interpolation.

<span style="color: #0000ff;">print config.get(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',{<span style="color: #800000;">'<span style="color: #800000;">bar<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">Documentation<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">baz<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">evil<span style="color: #800000;">'})

<span style="color: #008000;">#<span style="color: #008000;"> New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
config = ConfigParser.SafeConfigParser({<span style="color: #800000;">'
<span style="color: #800000;">bar
<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">Life<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">baz<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">hard<span style="color: #800000;">'<span style="color: #000000;">})
config.read(<span style="color: #800000;">'<span style="color: #800000;">example.cfg<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #0000ff;">print config.get(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">foo<span style="color: #800000;">') <span style="color: #008000;">#<span style="color: #008000;"> -> "Python is fun!"
config.remove_option(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">bar<span style="color: #800000;">'<span style="color: #000000;">)
config.remove_option(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">baz<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">print config.get(<span style="color: #800000;">'<span style="color: #800000;">Section1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">foo<span style="color: #800000;">') <span style="color: #008000;">#<span style="color: #008000;"> -> "Life is hard!"

1
>>> >>> >>> sample_config = <span style="color: #800000;">"""<span style="color: #800000;">
... [mysqld]
... user = mysql
... pid-file = /var/run/mysqld/mysqld.pid
... skip-external-locking
... old_passwords = 1
... skip-bdb
... skip-innodb
...
<span style="color: #800000;">"""

config = ConfigParser.RawConfigParser(allow_no_value=<span style="color: #000000;">True)
>>><span style="color: #000000;"> config.readfp(io.BytesIO(sample_config))

>>> <span style="color: #008000;">#<span style="color: #008000;"> Settings with values are treated as before:

config.get(<span style="color: #800000;">"<span style="color: #800000;">mysqld<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #800000;">'<span style="color: #800000;">mysql<span style="color: #800000;">'

<span style="color: #008000;">#<span style="color: #008000;"> Settings without values provide None:
config.get(<span style="color: #800000;">"<span style="color: #800000;">mysqld<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">skip-bdb<span style="color: #800000;">"<span style="color: #000000;">)

>>> <span style="color: #008000;">#<span style="color: #008000;"> Settings which aren't specified still raise an error:

config.get(<span style="color: #800000;">"<span style="color: #800000;">mysqld<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">does-not-exist<span style="color: #800000;">"<span style="color: #000000;">)
Traceback (most recent call last):
...
ConfigParser.NoOptionError: No option <span style="color: #800000;">'<span style="color: #800000;">does-not-exist<span style="color: #800000;">' <span style="color: #0000ff;">in section: <span style="color: #800000;">'<span style="color: #800000;">mysqld<span style="color: #800000;">'

<div class="cnblogs_code" onclick="cnblogs_code_show('ee5b8f44-556c-4305-ac70-e7323ae1f4b6')">
<img id="code_img_closed_ee5b8f44-556c-4305-ac70-e7323ae1f4b6" class="code_img_closed" src="https://www.52php.cn/res/2019/02-14/22/1c53668bcee393edac0d7b3b3daff1ae.gif" alt=""><img id="code_img_opened_ee5b8f44-556c-4305-ac70-e7323ae1f4b6" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('ee5b8f44-556c-4305-ac70-e7323ae1f4b6',event)" src="https://www.52php.cn/res/2019/02-14/22/405b18b4b6584ae338e0f6ecaf736533.gif" alt=""><div id="cnblogs_code_open_ee5b8f44-556c-4305-ac70-e7323ae1f4b6" class="cnblogs_code_hide">

with open(,encoding= line  line.startswith(= line.split(<span style="color: #0000ff;">import<span style="color: #000000;"> configparser

config =<span style="color: #000000;"> configparser.ConfigParser()

config[<span style="color: #800000;">"<span style="color: #800000;">DEFAULT<span style="color: #800000;">"] = {<span style="color: #800000;">'<span style="color: #800000;">ServerAliveInterval<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">45<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">Compression<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">yes<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">CompressionLevel<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">9<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">ForwardX11<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">yes<span style="color: #800000;">'<span style="color: #000000;">
}

config[<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">'] = {<span style="color: #800000;">'<span style="color: #800000;">User<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">hg<span style="color: #800000;">'<span style="color: #000000;">}

config[<span style="color: #800000;">'<span style="color: #800000;">topsecret.server.com<span style="color: #800000;">'] = {<span style="color: #800000;">'<span style="color: #800000;">Host Port<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">50022<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">ForwardX11<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">no<span style="color: #800000;">'<span style="color: #000000;">}

with open(<span style="color: #800000;">'<span style="color: #800000;">example.ini<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">w<span style="color: #800000;">'<span style="color: #000000;">) as configfile:
config.write(configfile)

<span style="color: #0000ff;">import<span style="color: #000000;"> configparser

config =<span style="color: #000000;"> configparser.ConfigParser()
<span style="color: #0000ff;">print(config.sections()) <span style="color: #008000;">#<span style="color: #008000;"> []
config.read(<span style="color: #800000;">'<span style="color: #800000;">example.ini<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">print(config.sections()) <span style="color: #008000;">#<span style="color: #008000;"> ['bitbucket.org','topsecret.server.com']

<span style="color: #0000ff;">print(<span style="color: #800000;">'<span style="color: #800000;">bytebong.com<span style="color: #800000;">' <span style="color: #0000ff;">in config) <span style="color: #008000;">#<span style="color: #008000;"> False
<span style="color: #0000ff;">print(<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">' <span style="color: #0000ff;">in config) <span style="color: #008000;">#<span style="color: #008000;"> True
<span style="color: #0000ff;">print(config[<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">'][<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"]) <span style="color: #008000;">#<span style="color: #008000;"> hg
<span style="color: #0000ff;">print(config[<span style="color: #800000;">'<span style="color: #800000;">DEFAULT<span style="color: #800000;">'][<span style="color: #800000;">'<span style="color: #800000;">Compression<span style="color: #800000;">']) <span style="color: #008000;">#<span style="color: #008000;">yes
<span style="color: #0000ff;">print(config[<span style="color: #800000;">'<span style="color: #800000;">topsecret.server.com<span style="color: #800000;">'][<span style="color: #800000;">'<span style="color: #800000;">ForwardX11<span style="color: #800000;">']) <span style="color: #008000;">#<span style="color: #008000;">no
<span style="color: #0000ff;">print(config[<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">']) <span style="color: #008000;">#<span style="color: #008000;">
<span style="color: #0000ff;">for key <span style="color: #0000ff;">in config[<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">']: <span style="color: #008000;">#<span style="color: #008000;"> 注意,有default会默认default的键
<span style="color: #0000ff;">print<span style="color: #000000;">(key)
<span style="color: #0000ff;">print(config.options(<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">')) <span style="color: #008000;">#<span style="color: #008000;"> 同for循环,找到'bitbucket.org'下所有键
<span style="color: #0000ff;">print(config.items(<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">')) <span style="color: #008000;">#<span style="color: #008000;">找到'bitbucket.org'下所有键值对
<span style="color: #0000ff;">print(config.get(<span style="color: #800000;">'<span style="color: #800000;">bitbucket.org<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">compression<span style="color: #800000;">')) <span style="color: #008000;">#<span style="color: #008000;"> yes get方法Section下的key对应的value

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

描述