C/C++教程

configparser模块

本文主要是介绍configparser模块,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

来看一个好多软件的常见配置文件格式如下

 

[DEFAULT] 是每个section共有的

 

 1 [DEFAULT]
 2 serveraliveinterval = 45
 3 compression = yes
 4 compressionlevel = 9
 5 forwardx11 = yes
 6 
 7 [bitbucket.org]
 8 user = hg
 9 maxusers = 110
10 
11 [topsecret.server.com]
12 port = 50022
13 forwardx11 = no
14 
15 [group1]
16 name = lemi
17 age = 21
18 
19 [group2]
20 name = kevin

解析配置文件

 

>>> import configparser # 导入模块
>>> config = configparser.ConfigParser()  #实例化(生成对象)
>>> config.sections()  #调用sections方法
[]
>>> config.read('example.ini')  # 读配置文件(注意文件路径)
['example.ini']
>>> config.sections() #调用sections方法(默认不会读取default)
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config #判断元素是否在sections列表内
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) # for循环 bitbucket.org 字典的key
...
user
compressionlevel
serveraliveinterval
compression
forwardx11

 

增删改查语法 

import configparser
conf = configparser.ConfigParser()
conf.read("conf.ini",encoding='utf-8')
for k,v in conf['bitbucket.org'].items():
    print(k,v)
>>>
user hg
maxusers 110
serveraliveinterval 45
compression yes
compressionlevel 9
forwardx11 yes

print(conf.options('bitbucket.org'))
>>>['user', 'maxusers', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

print(conf['group1']['age'])
>>>21

print(dir(conf))
>>>'add_section', 'clear', 'converters', 'default_section', 'defaults', 'get', 'getboolean', 'getfloat', 'getint', 'has_option', 'has_section', 'items', 'keys', 'options', 
'optionxform', 'pop', 'popitem', 'read', 'read_dict', 'read_file', 'read_string', 'readfp', 'remove_option', 'remove_section', 'sections', 'set', 'setdefault', 'update', 'values', 'write']

# 添加模块,并赋值
conf.add_section('group2')
conf['group2']['name'] = 'kevin'
conf['group2']['age'] = '22'
conf.write(open('conf.ini','w'))

conf.add_section('group3')
#赋值,修改
conf.set('group3','name','fadewalk')
conf.write(open('conf.ini','w'))

#删除section模块
conf.remove_section('group3')
conf.write(open('conf.ini','w'))

#删除选项
conf.remove_option('group2','age')
conf.write(open('conf.ini','w'))

  

练习题

通过configparser模块完成以下功能

文件名my.cnf

[DEFAULT]

[client]
port = 3306
socket = /data/mysql_3306/mysql.sock

[mysqld]
explicit_defaults_for_timestamp = true
port = 3306
socket = /data/mysql_3306/mysql.sock
back_log = 80
basedir = /usr/local/mysql
tmpdir = /tmp
datadir = /data/mysql_3306
default-time-zone = '+8:00'

修改时区 default-time-zone = '+8:00' 为 校准的全球时间 +00:00
删除 explicit_defaults_for_timestamp = true
为DEFAULT增加一条 character-set-server = utf8


import configparser

config = configparser.ConfigParser()
config.read('my.conf')

config.set('mysqld','default-time-zone','+00:00')
config.remove_option('mysqld','explicit_defaults_for_timestamp')
config.set('DEFAULT','character-set-server','utf8')

config.write(open('my.conf', "w"))
print(config['mysqld']['default-time-zone'] )

 

 

 

这篇关于configparser模块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!