python之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之ConfigParser的使用。的更多相关文章
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- python 之ConfigParser
ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
随机推荐
- LeetCode:数据库技术【175-178】
LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...
- docker学习笔记2--对镜像/容器的命令操作
Docker启动一个Centos镜像 我们下载完成一个Centos镜像之后,开始启动 docker run -d -i -t <imageID> /bin/bash 这样就能启动一个一直停 ...
- RHEL 5 安装gcc
rpm -ivh kernel-headers... rpm -ivh glibc-headers... rpm -ivh glibc-devel... rpm -ivh libgomp.. rpm ...
- 【LeetCode】【动态规划】Edit Distance
描述 Given two words word1 and word2, find the minimum number of operations required to convert word1 ...
- 【leetcode刷题笔记】Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- ubuntu: lightdm 登录root超级管理员方法
ubuntu 12.04 lts 默认是不允许root登录的, 在登录窗口只能看到普通用户和访客登录. 以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后, 修改系统配置文件需要切换到超级 ...
- Qt5.4.1移植到arm——Linuxfb篇
Qt5与Qt4对比有很大的改变,其最大的特性在于模块化,并且很明显的是不再见到Qt4用到的qws,Qt5新增了QPA系统,基于QPA使得Qt5移 植到一个新平台非常简单而又具有极强的底层扩展能力:同时 ...
- MODBUS协议 一种问答方式的通信协议
源:MODBUS协议 一种问答方式的通信协议 ModBus通信系统协议
- 关于pycharm中的requirements.txt文件
作用:记录所有所依赖的第三方模块,方便迁移到不同的环境中后,防止缺少模块,或因为所依赖的第三方模块版本不同引起不必要的问题 生成命令:pip freeze > requirements.txt ...
- 20145230《Java程序设计》第3周学习总结
20145230 <Java程序设计> 第3周学习总结 教材学习内容总结 String s=new String();第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定 ...