day5模块学习--configparser模块
使用ConfigParser模块读写ini文件(http://blog.csdn.net/linda1000/article/details/11729561)
ConfigParserPython的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。模块所解析的ini配置文件是由多个section构成,每个section名用中括号‘[]’包含,每个section下可有多个配置项类似于key-value形式,例如:
ConfigParser模块以ConfigParser类为例,其操作基本分为三类:1)初始化;2)读取配置;3)写入配置。
1. ConfigParser 初始化
使用ConfigParser 首选需要初始化实例,并读取配置文件:
cf = ConfigParser.ConfigParser() cf.read("配置文件名")
2.基本的读取配置文件
-read(filename)直接读取ini文件内容;
-sections()得到所有的section,并以列表的形式返回
-options(section)得到该section的所有option
-items(section)得到该section的所有键值对;
-get(section,option)得到section中的option的值,返回为string类型
-getint(section,option)得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat()函数。
3.基本的写入配置文件
-add_section(section)添加一个新的section;
-set(section,option,value)对section中的option进行设置,需要调用write将内容写入配置文件;
-write(strout)将对configparser类的修改写入。
例如:
test.conf #文件名
[sec_a]
a_key1
=
20
a_key2
=
10
[sec_b]
b_key1
=
121
b_key2
=
b_value2
b_key3
=
$r
b_key4
=
127.0
.
0.1
import configparser #初始化
cf = configparser.ConfigParser() #读取加载文件
cf.read('test.conf') # 读取配置文件
secs = cf.sections()
print("secs",secs)
opts = cf.options('sec_a')
print("options",opts) kvs = cf.items("sec_a")
print("sec_a",kvs) #read by type
str_val = cf.get("sec_a","a_key1")
int_val = cf.getint("sec_a","a_key2")
print("str_val",str_val)
print("int_val",int_val) #写入配置文件
#update value
cf.set("sec_b","b_key3","new_$r")
#set a new value
cf.set("sec_b","b_newkey","new_value")
#create a new section
cf.add_section("a_new_section")
cf.set("a_new_section","new_key","new_value") #write back to configure file
cf.write(open("test.conf","w"))
运行结果如下:
secs ['sec_a', 'sec_b']
options ['a_key1', 'a_key2']
sec_a [('a_key1', '20'), ('a_key2', '10')]
str_val 20
int_val 10
下面用python生成一个configparser的文件格式:
import configparser config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
day5模块学习--configparser模块的更多相关文章
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...
- python基础14 ---函数模块4(configparser模块)
configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- day5模块学习 -- os模块学习
python基础之模块之os模块 os模块 os模块的作用: os,语义为操作系统,所以肯定就是操作系统相关的功能了,可以处理文件和目录这些我们日常手动需要做的操作,就比如说:显示当前目录下所有文件/ ...
- Python模块学习 - ConfigParser
配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- python模块之configparser模块
configparser模块:用于按一定格式创建配置文件 创建 import configparser config = configparser.ConfigParser() config['DEF ...
- Pyhton之subprocess模块和configparser模块
一.subprocess模式 # import os # while True: # cmd=input('>>').strip() # if not cmd:continue # if ...
随机推荐
- NO.3day 网络基础
网络基础 1.互联网协议 概念:通过互联网传输数据的标准. 功能:定义计算机如何接入internet,以及接入internet的计算机通信的标准. 2.OSI五层模型 应用层--传输层--网络层--数 ...
- GO_05:GO语言基础map与函数
1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...
- git简单使用总结
一.git配置 git config 1.git config --global user.name "ken" 配置用户名2.git config --global user.e ...
- vuejs实现数据驱动视图原理
什么是数据驱动 数据驱动是vuejs最大的特点.在vuejs中,所谓的数据驱动就是当数据发生变化的时候,用户界面发生相应的变化,开发者不需要手动的去修改dom. 比如说我们点击一个button,需要元 ...
- MySQL触发器的正确使用与案例分析
以下的文章主要向大家讲述的是MySQL触发器的实际使用详细说明与实际案例分析,同时本文也列举了一些在MySQL触发器的实际式操作中的代码,以下就是文章的详细内容介绍,望大家借鉴. 触发器案例 mysq ...
- NOIP2011 提高组 Day1
自测:8:27——11:51 实际得分:100+60+20=180 期望得分:100+60+40=200 T3读错题,失20 http://cogs.pro/cogs/page/page.php?ai ...
- 蓝桥杯 剪邮票 DFS (不错的题目)
剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)比如,[图2.jpg],[图3.jpg]中,粉红色所示部分就是合格的 ...
- 二、Kafka基础实战:消费者和生产者实例
一.Kafka消费者编程模型 1.分区消费模型 分区消费伪代码描述 main() 获取分区的size for index =0 to size create thread(or process) co ...
- Django进阶(路由系统、中间件、缓存、Cookie和Session、Ajax发送数据
路由系统 1.每个路由规则对应一个view中的函数 url(r'^index/(\d*)', views.index), url(r'^manage/(?P<name>\w*)/(?P&l ...
- classList属性
1.传统方法: 在操作类名的时候,需要通过className属性添加.删除和替换类名.如下面例子: ? 1 <div class="bd user disabled"> ...