python基础2-configparser模块

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

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

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

生成这样一个文档

 1 import configparser
 2  
 3 config = configparser.ConfigParser()
 4 config["DEFAULT"] = {'ServerAliveInterval': '45',
 5                       'Compression': 'yes',
 6                      'CompressionLevel': '9'}
 7  
 8 config['bitbucket.org'] = {}
 9 config['bitbucket.org']['User'] = 'hg'
10 config['topsecret.server.com'] = {}
11 topsecret = config['topsecret.server.com']
12 topsecret['Host Port'] = '50022'     # mutates the parser
13 topsecret['ForwardX11'] = 'no'  # same here
14 config['DEFAULT']['ForwardX11'] = 'yes'
15 with open('example.ini', 'w') as configfile:
16    config.write(configfile)

读取

补充:

print(config.defaults()) #有序字典
#OrderedDict([('compressionlevel', '9'), ('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'yes')])
import configparser
config = configparser.ConfigParser()
config.read("example.ini")
secs = config.sections()
print(secs)

for key in config["topsecret.server.com"]:print(key)
'''
返回
host port
forwardx11
compressionlevel
serveraliveinterval
compression
'''
section_name = config.sections()[1]
for i,v in config[section_name].items():
    print(i,v)
'''
返回
host port 50022
forwardx11 no
compressionlevel 9
serveraliveinterval 45
compression yes
'''

  

 

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

增删改查

#读取
import configparser
config = configparser.ConfigParser()
config.read("example.ini")
sec = config.sections()
print(sec)
#返回['bitbucket.org', 'topsecret.server.com']
print(config.options('topsecret.server.com'))
#返回['host port', 'forwardx11', 'compressionlevel', 'compression', 'serveraliveinterval']
print(config.items('topsecret.server.com'))
#返回[('compressionlevel', '9'), ('compression', 'yes'), ('serveraliveinterval', '45'), ('forwardx11', 'no'), ('host port', '50022')]

print(type(config.get('topsecret.server.com','host port')))
#<class 'str'>
print(type(config.getint('topsecret.server.com','host port')))
#<class 'int'>


#改写
import configparser
config = configparser.ConfigParser()
config.read('example.ini')
#必须有read这一行,否则会清空整个文件
sec1 = config.remove_section('topsecret.server.com')
config.write(open('example.ini','w'))

import configparser
config = configparser.ConfigParser()
config.read('example.ini')
# print(config.has_section("topsecret.server.com"))

# print(config.add_section("www.baidu.com"))
# config.write(open("example.ini",'w'))

# config.set("www.baidu.com",'name','luoliyu')
# config.write(open('example.ini','w'))

# config.remove_option("www.baidu.com",'name')
# config.write(open("example.ini",'w'))

  

 

posted @ 2017-09-13 16:18  larlly  阅读(162)  评论(0)    收藏  举报