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

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

1
2
3
4
5
6
7
8
9
10
11
12
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
 
[bitbucket.org]
User = hg
 
[topsecret.server.com]
Port = 50022
ForwardX11 = no
 

如果想用python生成一个这样的文档怎么做呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
import configparser  #in Python 2.x ConfigParser
config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
# topsecret = config['topsecret.server.com']
# topsecret['Host Port'] = '50022' # mutates the parser
# topsecret['ForwardX11'] = 'no' # same here
'上面注释的部分和下面的方法等价'
config['topsecret.server.com']['Host Port']='50022'
config['topsecret.server.com']['ForwardX11']='no' config['DEFAULT']['ForwardX11'] = 'yes' #这边是对上面的DEFAULT追加了一条记录 with open('example.ini', 'w') as configfile:
config.write(configfile)

写完了还可以再读出来哈。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org''topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Host Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

configparser增删改查语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[section1]
k1 = v1
k2:v2
  
[section2]
k1 = v1
#以上为配置文件显示的结构
 
import ConfigParser
  
config = ConfigParser.ConfigParser()
config.read('i.cfg')
  
# ########## 读 ##########
#secs = config.sections()
#print(secs)
#options = config.options('group2')
#print(options)
 
#item_list = config.items('group2')
#print(item_list)
  
#val = config.get('group1','key')
#val = config.getint('group1','key')
  
# ########## 改写 ##########
#sec = config.remove_section('group1')
#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"))

最新文章

  1. UIViewContentMode各类型效果
  2. Ubuntu下配置samba实现文件夹共享
  3. php提示Fatal error: Call to undefined function imagecreate()
  4. C++求最小公倍数
  5. Leetcode 210 Course Schedule II
  6. 【原创】省市二级联动纯javascript
  7. centos 卸载自带的 java
  8. 用showModalDialog写的简单弹出框传参与反参
  9. spring 资源加载使用说明
  10. Spring连接池的常用配置
  11. pl/sql中if的用法
  12. MTK机器原始OTA更新方法
  13. 开启 IPv6 新时代,升级后的 IPv6 厉害在哪?
  14. 【XSY2843】「地底蔷薇」 NTT什么的 扩展拉格朗日反演
  15. elasticsearch的监控脚本
  16. W7500P硬件TCP/IP+硬件物理层PHY+Cortex-M0处理器(48MHZ)
  17. CPUFreq驱动
  18. transform CSS3 2D知识点汇总
  19. 使用web api开发微信公众号,调用图灵机器人接口(一)
  20. 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例

热门文章

  1. mysql慢查询分析工具比较与实战
  2. Base64编码与解码原理
  3. eolinker测试增强
  4. PTA数据结构与算法题目集(中文) 7-4
  5. python-nmap 使用基础
  6. MyBatis(五):分页
  7. HAproxy 基础配置
  8. 双色球的Python实现
  9. 搞搞hibernate.current_session_context_class
  10. AJ学IOS 之小知识iOS启动动画_Launch Screen的运用