python ConfigParse模块(转)
2015-06-30 19:30
597 查看
最近写程序要用到配置文件,那么配置文件的解析就很重要了,下文转自chinaunix
一、ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
[/code]
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
[/code]
三、ConfigParser 常用方法
1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
[/code]
将输出(以下将均以简介中配置文件为例):
[/code]
2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
[/code]
将输出:
[/code]
3. 获取指定section 的配置信息。
[/code]
将输出:
[/code]
4. 按照类型读取指定section 的option 信息。
同样的还有getfloat、getboolean。
[/code]
将输出:
[/code]
5. 设置某个option 的值。(记得最后要写回)
[/code]
6.添加一个section。(同样要写回)
[/code]
7. 移除section 或者option 。(只要进行了修改就要写回的哦)
#!/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"))
[/code]
一、ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
[code]1: [db] 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
[/code]
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
[code]1: cf = ConfigParser.ConfigParser() 2: cf.read("配置文件名")
[/code]
三、ConfigParser 常用方法
1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
[code]1: s = cf.sections() 2: print 'section:', s
[/code]
将输出(以下将均以简介中配置文件为例):
[code]1: section: ['db', 'concurrent']
[/code]
2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
[code]1: o = cf.options("db") 2: print 'options:', o
[/code]
将输出:
[code]1: options: ['db_host', 'db_port', 'db_user', 'db_pass']
[/code]
3. 获取指定section 的配置信息。
[code]1: v = cf.items("db") 2: print 'db:', v
[/code]
将输出:
[code]1: db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]
[/code]
4. 按照类型读取指定section 的option 信息。
同样的还有getfloat、getboolean。
[code]1: #可以按照类型读取出来 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
[/code]
将输出:
[code]1: db_host: 127.0.0.1 2: db_port: 22 3: db_user: root 4: db_pass: rootroot 5: thread: 10 6: processor: 20
[/code]
5. 设置某个option 的值。(记得最后要写回)
[code]1: cf.set("db", "db_pass", "zhaowei") 2: cf.write(open("test.conf", "w"))
[/code]
6.添加一个section。(同样要写回)
[code]1: cf.add_section('liuqing') 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"))
[/code]
7. 移除section 或者option 。(只要进行了修改就要写回的哦)
[code]1: cf.remove_option('liuqing','int') 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"))
[/code]
相关文章推荐
- Python-strip()方法
- python中的元类metaclass
- Python文件操作
- Python读写文件实际操作的五大步骤
- python super()
- 用PDB库调试Python程序
- Python模块的介绍与使用
- python的keyword模块用法实例分析
- 算法导论—分治法计算最大子数组(Python)
- Python实现监控程序执行时间并将其写入日志的方法
- Python IDE pycharm
- python实现爬取千万淘宝商品的方法
- python简单判断序列是否为空的方法
- python检查序列seq是否含有aset中项的方法
- Python调试
- python判断一个集合是否包含了另外一个集合中所有项的方法
- python中的__init__ 、__new__、__call__等内置函数的剖析
- python过滤字符串中不属于指定集合中字符的类实例
- Python Django 开发 4 ORM
- Python学习笔记2_基础知识