ConfigParser简介
一、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"))
ConfigParser简介的更多相关文章
- python 之ConfigParser
ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...
- python ConfigParser配置读写
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...
- python ConfigParser、shutil、subprocess、ElementTree模块简解
ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...
- 解析配置文件ConfigParser模块
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...
- python之configparser模块详解--小白博客
configparse模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似 ...
- Python 之ConfigParser模块
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...
- Python 之ConfigParser 学习笔记
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...
- python 序列化 pickle shelve json configparser
1. 什么是序列化 我们把变量从内存中变成可存储或传输的过程称之为序列化. 序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上. 反过来,把变量内容从序列化的对象重新读到内存里称 ...
- python ConfigParser 配置读写
我们写程序的时候一般都会写程序包的时候,很少硬编码写在一起,这有个很好的配置文件包. 参考 ConfigParser 和 ConfigParser 的使用 一.ConfigParser 简介 C ...
随机推荐
- Real-Time Rendering (2) - 变换和矩阵(Transforms and Matrics)
http://blog.csdn.net/silangquan/article/details/9970673 提要 在图形的计算中,比如旋转.缩放.平移.投影等操作,矩阵都扮演着极其重要的角色,它是 ...
- window内置对象学习
1.location:本页面的location对象 对象属性图示: 对象属性: 对象方法: 2.history:是本页面的浏览历史 history对象记录了用户曾经浏览过的页面(URL),并可以实现浏 ...
- Surface Pro 4远程桌面分辨率问题
Surface Pro 4是非常收欢迎的笔记本电脑.但我们这些技术人员在使用中有一点非常不方便: Surface Pro 4的分辨率非常高,如果用Surface Pro 4远程桌面到远端的一台机器,因 ...
- Erlang pool management -- Emysql pool optimize
在上一篇关于Emysql pool (http://www.cnblogs.com/--00/p/4281938.html)的分析的最后提到 现在的emysql_conn_mgr gen_server ...
- nginx错误
在开发的时候遇到nginx错误 网上找了半天也没有找到解决方案: 先查看了一下nginx错误日志 cat /usr/local/nginx/logs/error.log 然后发现看不太懂 那么只能重启 ...
- Disconf —— 来自百度的分布式配置管理平台
摘要 为了更好的解决分布式环境下多台服务实例的配置统一管理问题,本文提出了一套完整的分布式配置管理解决方案(简称为disconf[4],下同).首先,实现了同构系统的配置发布统一化,提供了配置服务se ...
- Java-API:un-java.util.Set
ylbtech-Java-API:java.util.Set 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https://docs.oracle.com ...
- git学习 删除远程分支
2种方法删除远端分支: git branch -r -d origin/branch-name // -r: 远端: -d:删除 git push origin :branch-name ...
- 第九课 go的循环语句
1 for语句的三种形式 /* for 循环 */ ; a < ; a++ { fmt.Printf("a 的值为: %d\n", a) } var a, b int = 1 ...
- 基于RFC5321使用ncat发送邮件
今天和同事学习到的这个方法,学习了,记录一下: [root@localhost ~]# ncat TeamServer.localdomain ESMTP Postfix EHLO l00.win - ...