码迷,mamicode.com
首页 > 编程语言 > 详细

Python configparser模块

时间:2017-11-05 13:55:58      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:删除   版本   read   str   with open   style   color   efault   配置文件   

 

configparser模块

 

 

一.configparser模块

用于生成和修改常见配置文档,但那个钱模块名称在python3.x版本中变更为configparser。

 技术分享

 

 

1.生成一个配置。

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {‘serveraliveinterval‘:‘45‘,

‘compression‘:‘yes‘,

‘compressionlevel‘:‘9‘

}

config[‘bitbucket.org‘] = {}

config[‘bitbucket.org‘][‘user‘] = ‘hg‘

with open(‘example.ini‘,‘w‘) as configfile:

config.write(configfile)

注:生成配置文件example.ini

 

 

2.读取配置文件

import configparser

conf = configparser.ConfigParser()

conf.read("example.ini")

print(conf.defaults())

print(conf.sections())

print(conf[‘bitbucket.org‘][‘user‘])


注:conf.defaults:读取的是defaults以字典类型读取

注:conf.sections:读取的是节点,不包含defaults。

注:conf[‘bitbucket.org‘][‘user‘]:则是直接读取节点下内容。

 

4.删除配置文件内容。

import configparser

conf = configparser.ConfigParser()

conf.read("example.ini")

print(conf.defaults())

print(conf.sections())

print(conf[‘bitbucket.org‘][‘user‘])

sec = conf.remove_section(‘bitbucket.org‘)

conf.write(open(‘exmple2.cfg‘,"w"))

注:删除并创建备份新的文件内。

Python configparser模块

标签:删除   版本   read   str   with open   style   color   efault   配置文件   

原文地址:http://www.cnblogs.com/xiangsikai/p/7787149.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!