configparser 模块

import configparser

config = configparser.ConfigParser() #相当于有一个空字典config

import 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['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
#--------------------------------------增删改查
import configparser

config = configparser.ConfigParser()

# -------------- 查
config.read('example.ini') #读取文件
# print(config.sections()) #打印除defult以外的块名
# print(config['bitbucket.org']['user'])
# for key in config['bitbucket.org']:
# print(key)


#---增删改
# config.add_section('doudou') #增加块
# config.set('doudou','gou','12') #对doudou块里面增加一个gou键,其对应的值是12

config.remove_section('doudou') #删除块
config.remove_option('bitbucket.org','user')


config.write(open('new_example.ini','w'))
posted @ 2018-04-12 22:56  阜阳小全  阅读(97)  评论(0编辑  收藏  举报