python configparser模块
<h1 id="简介">简介
该模块的就是使用模块中的 配置文件有不同的片段组成和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
方法:下面这三种方式使用时,切记 在调用这三个函数时,切记这三个函数会将调用
dict_type:使用新的section的键值对 allow_no_value :默认是False,如果是True,表示可以接收空值(None) <span style="color: #0000ff;">return:对象 |
描述 |
---|
对象的操作可以分为两大类,一种是对配置文件的操作,另一种是对读取后数据流的操作。
读取配置文件:
描述 |
---|
描述 |
---|
描述 |
---|
描述 |
---|
描述 |
---|
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;">')])
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;">
}
<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.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.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;">'})
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!"
... [mysqld]
... user = mysql
... pid-file = /var/run/mysqld/mysqld.pid
... skip-external-locking
... old_passwords = 1
... skip-bdb
... skip-innodb
... <span style="color: #800000;">"""
>>><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 = ConfigParser.RawConfigParser(allow_no_value=<span style="color: #000000;">True)
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;">'
<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">
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;">'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
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!