ConfigParser模块,主要应用于对php.ini等格式的配置文件内容读取和生成。删改较少用

 *.ini配置文件样例 

创建默认的文件样例

读取文件内容

read()

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
[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"))

猜你喜欢

转载自www.cnblogs.com/zhangmingda/p/9132645.html