python ConfigParse模块(转)
最近写程序要用到配置文件,那么配置文件的解析就很重要了,下文转自chinaunix
一、ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
2: db_host = 127.0.0.1
3: db_port = 22
4: db_user = root
5: db_pass = rootroot
6:
7: [concurrent]
8: thread = 10
9: processor = 20
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
2: cf.read("配置文件名")
三、ConfigParser 常用方法
1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
2: print 'section:', s
将输出(以下将均以简介中配置文件为例):
2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
2: print 'options:', o
将输出:
3. 获取指定section 的配置信息。
2: print 'db:', v
将输出:
4. 按照类型读取指定section 的option 信息。
同样的还有getfloat、getboolean。
2: db_host = cf.get("db", "db_host")
3: db_port = cf.getint("db", "db_port")
4: db_user = cf.get("db", "db_user")
5: db_pass = cf.get("db", "db_pass")
6:
7: # 返回的是整型的
8: threads = cf.getint("concurrent", "thread")
9: processors = cf.getint("concurrent", "processor")
10:
11: print "db_host:", db_host
12: print "db_port:", db_port
13: print "db_user:", db_user
14: print "db_pass:", db_pass
15: print "thread:", threads
16: print "processor:", processors
将输出:
2: db_port: 22
3: db_user: root
4: db_pass: rootroot
5: thread: 10
6: processor: 20
5. 设置某个option 的值。(记得最后要写回)
2: cf.write(open("test.conf", "w"))
6.添加一个section。(同样要写回)
2: cf.set('liuqing', 'int', '15')
3: cf.set('liuqing', 'bool', 'true')
4: cf.set('liuqing', 'float', '3.1415')
5: cf.set('liuqing', 'baz', 'fun')
6: cf.set('liuqing', 'bar', 'Python')
7: cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')
8: cf.write(open("test.conf", "w"))
7. 移除section 或者option 。(只要进行了修改就要写回的哦)
2: cf.remove_section('liuqing')
3: cf.write(open("test.conf", "w"))
点击(此处)折叠或打开
- #!/usr/bin/env python
- from ConfigParser import ConfigParser
- CONFIGFILE="f.txt"
- config=ConfigParser()
- config.read(CONFIGFILE)
- print config.get('messages','greeting')
- radius=input(config.get('messages','questions')+' ')
- print config.get('messages','result')
- print config.getfloat('numbers','pi')*radius**2
- s=config.sections()
- print'section: ',s
- o=config.options('messages')
- print'messages option: ',o
- v=config.items("messages")
- print'message de xinxi: ',v
- config.add_section('liuyang1')
- config.set('liuyang1','int','15')
- config.set('liuyang'1,'hhhh','hello world')
- config.write(open("f.txt","w"))
- print config.get('liuyang1','int')
- print config.get('liuyang1','hhhh')
- #!/usr/bin/env python
- import ConfigParser
- import sys
- config=ConfigParser.ConfigParser()
- config.add_section("book1")
- config.set("book1","title","hello world")
- config.set("book1","aut","log")
- config.write(open("f.txt","w"))
python ConfigParse模块(转)的更多相关文章
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- python模块: hashlib模块, configparse模块, logging模块,collections模块
一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...
- Python模块-configparse模块
configparse模块用来解析配置文件 配置文件 [DEFAULT] port = 3306 socket = /tmp/mysql.sock [mysqldump] max_allowed_pa ...
- Python进阶-XVV hashlib模块、configparse模块、logging模块
1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...
- python学习-58 configparse模块
configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...
- 【转】Python3 configparse模块(配置)
[转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...
- 14 ConfigParse模块
1.ConfigParse模块的基本概念 此模块用于生成和修改常见配置文档. ConfigParser 是用来读取配置文件的包. 配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- configParse模块
一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...
- python常用模块-1
一.认识模块 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.py文 ...
随机推荐
- 解决mysql不能通过'/tmp/mysql.sock 连接的问题
解决方法:php标准配置正是通过'/tmp/mysql.sock',但一些mysql安装方法将mysql.sock放在/var/lib/mysql.sock或者其他地方,你可以通过修改/etc/my. ...
- python 简单的文件下载
需要使用urllib2库 import urllib2def download(url, szFileName = ""): #szFileName:下载文件到的目标路径 if s ...
- SQL学习笔记。
数据库视图: 视图是虚表,是从一个或几个基本表(或视图)中导出的表,在系统的数据字典中仅存放了视图的定义,不存放视图对应的数据. 视图是原始数据库数据的一种变换,是查看表中数据的另外一种方式.可以将视 ...
- 0604-Zuul构建API Gateway-Zuul的回退
一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#hystrix-f ...
- java-mybaits-00501-案例-映射分析-订单商品数据模型
1.数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空字段.外键 ...
- JavaScript工具库 lodash 中文文档 英文文档
https://lodash.com/docs/ 英文版 http://lodashjs.com/docs/ 中文版 http://www.css88.com/doc/lodash/ 中文版 ...
- nanomsg(ZeroMQ with C)
1.应用手册 https://github.com/nanomsg/nanomsg % mkdir build % cd build % cmake .. % cmake --build . % ct ...
- 解析button和input type=”button”的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
- 模块讲解----shutil模块(copy、压缩、解压)
作用与功能 主要用于文件的copy,压缩,解压 导入shuitl模块: import shutil copy方法 1.shutil.copyfileobj() 打开file1,并copy写入file ...
- listview与adapter用法
Android listview与adapter用法 listview与adapter用法 博客分类: android 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用 ...