1、ConfigParse模块的基本概念

此模块用于生成和修改常见配置文档

ConfigParser 是用来读取配置文件的包。

配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

文件名my.cnf.ini

[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'

2、解析配置文件

import configparser
config =configparser.ConfigParser() #初始化实例
config.read('my.cnf.ini')#读取配置文件
print(config.sections())#读取模块名到列表中,也就是[]中的内容 print(config.default_section)
>['client', 'mysqld']
#----------------------------------------------------------------------
```py
>>> 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
>>> config['bitbucket.org']['ForwardX11']
'yes'
```

3、增删改查语法

 
#----------------------------------------------------------------------------
# 修改时区 default-time-zone = '+8:00' 为 校准的全球时间 +00:00
config['mysqld']['default-time-zone'] = '+00:00'
config.write(open('my.cnf.ini','w')) # 删除 explicit_defaults_for_timestamp = true
config.remove_option('mysqld','explicit_defaults_for_timestamp = true')
config.write(open('my.cnf.ini','w')) # 为DEFAULT增加一条 character-set-server = utf8
config['DEFAULT']['character-set-server'] = 'utf8' # 与上面的语法效果一样config,set('DEFAULT','character-set-server','utf8') config.write(open('my.cnf.ini','w'))

#------------------------------------------------------------------------------------

```python
[group1] # 支持的两种分隔符“=”, “:”
k1 = v1
k2:v2
[group2]
k1 = v1
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('i.cfg') # ########## 读 ##########
#secs = config.sections()
#print(secs)
#options = config.options('group2') # 获取指定section的keys
#print(options) #item_list = config.items('group2') # 获取指定 section 的 keys & values ,key value 以元组的形式
#print(item_list) #val = config.get('group1','key') # 获取指定的key 的value
#val = config.getint('group1','key') # ########## 改写 ##########
#sec = config.remove_section('group1') # 删除section 并返回状态(true, false)
#config.write(open('i.cfg', "w")) # 对应的删除操作要写入文件才会生效

#sec = config.has_section('wupeiqi')
#sec = config.add_section('wupeiqi')
#config.write(open('i.cfg', "w")) # #config.set('group2','k1',11111)
#config.write(open('i.cfg', "w")) #config.remove_option('group2','age')
#config.write(open('i.cfg', "w"))
```
#-------------------------------------------------------------------------
[DEFAULT]
character-set-server = utf8 [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 = +00:00

最新文章

  1. DNS bind子域授权安装
  2. NOIP提高组2004 合并果子题解
  3. phpcms V9 首页模板文件解析
  4. [转]设置Android手机以使用ARM Streamline进行性能分析(一)
  5. 【读书笔记】读《JavaScript设计模式》之观察者模式
  6. PHP文件上传错误类型及说明
  7. 再见WCF
  8. Nginx 做负载均衡的几种轮询策略
  9. 第五篇:python高级之面向对象高级
  10. 编码神器 Sublime Text 包管理工具及扩展大全
  11. os内存使用管理之linux篇
  12. [Unity]Unity开发NGUI代码实现ScrollView(放大视图)
  13. js在(FF)中长字段溢出(自动换行)
  14. Python3 字典
  15. python使用装饰器对文件进行读写操作'及遍历文件目录
  16. js表单反显
  17. 使用第三方jar时出现的问题
  18. c# System.Console
  19. Android 开发 VectorDrawable 矢量图 (二)了解矢量图属性与绘制
  20. Java之枚举举例

热门文章

  1. 自适应巡航控制系统——ACC
  2. 【转】每天一个linux命令(52):ifconfig命令
  3. 【转】每天一个linux命令(42):kill命令
  4. 关于h5屏幕适配
  5. golang panic的捕获
  6. 在ASP.NET应用程序中使用身份模拟(Impersonation)
  7. 轻松理解execl系列函数
  8. RabbitMQ-2 工作队列
  9. PHP include 和 require 语句 (调用其他php文件进来的方法)
  10. oracle如何导出owner和tablespace