【python】模块——configparser模块

一、生成配置文件

1.导入configparser模块

import configparser

2.生成一个configparser对象

config = configparser.ConfigParser()     #生成一个config对象 config={}

3.生成config对象块,键值对的三种方法

# 直接生成config对象的块即键值对
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}

# 生成config对象的块
config['bitbucket.org'] = {}
# 生成config对象的键值
config['bitbucket.org']['User'] = 'hg'

# 另一种方法生成config对象的键值
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here

4.写入文件,没有时默认创建一个文件

with open('example.ini', 'w') as f:  #写入文件
    config.write(f)

5.文件:example.ini

[DEFAULT] 
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

 

 完整代码

import configparser

config = configparser.ConfigParser()     #生成一个config对象 config={}

# 生成config对象的块
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}

# 生成config对象的块
config['bitbucket.org'] = {}
# 生成config对象的键值
config['bitbucket.org']['User'] = 'hg'
# 另一种方法生成config对象的键值
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here

with open('example.ini', 'w') as f:  #写入文件
    config.write(f)
完整代码

 

二、增删改查

import configparser

config = configparser.ConfigParser()   #生成一个config对象 config={}

#---------------------------------------------查
# print(config.sections())   #查看 sections() 块名,需要先read(),直接查看的结果:[]


config.read('example.ini')    

print(config.sections())   #不显示DEFAULT
#['bitbucket.org', 'topsecret.server.com']

print('bytebong.com' in config)# False   判断块是否存在
# #
print(config['bitbucket.org']['User']) # hg  user不区分大小写

#遍历其他信息时自动遍历DEFAULT
for key in config['bitbucket.org']:
    print(key)

# 遍历块
print(config.options('bitbucket.org'))#遍历键名,放入一个列表
# ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org'))  #遍历键值对,放入一个元组
#  [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

print(config.get('bitbucket.org','compression'))   #yes,先遍历bitbucket.org块的键,查找compression的值


#---------------------------------------------删,改,增
#(config.write(open('i.cfg', "w")))

config.add_section('yuan')   #增加块yuan
config.set('yuan','k1','11111')    #改变块yuan的键值

config.remove_section('topsecret.server.com')   #删除块topsecret.server.com
config.remove_option('bitbucket.org','user')    #删除bitbucket.org块下的键值对user

config.write(open('i.cfg', "w"))    #写入文件,可覆盖原文件

 

注意:

1.查看 sections() 块名时,需要先read(),直接查看的结果返回空列表

2.遍历块名时,不显示DEFAULT

3.遍历任何块的键值时都会自动遍历的键值DEFAULT

config.read('example.ini')    

print(config.sections())   #不显示DEFAULT
#['bitbucket.org', 'topsecret.server.com']

#遍历其他信息时自动遍历DEFAULT
for key in config['bitbucket.org']:
    print(key)

————————————————————结束————————————————————

posted on 2018-07-23 23:35  索米尔  阅读(70)  评论(0)    收藏  举报

导航