Python之配置文件模块 ConfigParser
写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser
安装就不说了,pip一下即可,直接来个实例
配置文件 project.conf
[db]
host = '127.0.0.1'
port = 3306
user = 'root'
password = 'redhat'
db = 'project' [app]
threads = 4
cache_size = 8M [game]
speed = 100
脚本 project.py
#!/usr/bin/python
#coding: utf-8 import ConfigParser cf = ConfigParser.ConfigParser()
cf.read('project.conf') #所有section
print cf.sections() #显示某个sections下的选项
print cf.options('db') #键值方式显示section的值
print cf.items('db') #获得某个section的某个选项的值
host = cf.get('db', 'host')
print host ##get获得是string格式的,下面获得Int格式的和float格式的
port = cf.getint('db', 'port')
print port, type(port) port = cf.getfloat('db', 'port')
print port, type(port) ##获得boolean方式的值
play = cf.getboolean('game', 'play')
print play ##添加section
cf.add_section('redis')
print cf.sections() ##添加option
cf.set('redis', 'host', '127.0.0.1')
print cf.items('redis') ##修改option
cf.set('db', 'host', '192.168.1.1')
print cf.items('db') ##保存配置文件
cf.write(open('project.conf', 'w'))
总结方法:
read(filename) #读取配置文件
sections() #返回所有section
options(section) #返回section中的option
items(section) #返回sectiond的键值对
get(section, option) #返回某个section,某个option的值,类型是string
getint, getfloat, getboolean 等等返回的只是类型不同 修改配置
add_section(section) #添加section
set(section,option,value) #添加或者修改值
write(open(filename,'w')) #保存到配置文件
Python之配置文件模块 ConfigParser的更多相关文章
- Python之配置模块ConfigParser
http://docs.python.org/2/library/configparser.html http://www.cnblogs.com/sislcb/archive/2008/11/25/ ...
- Python解析配置文件模块:ConfigPhaser
算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...
- python之常用模块ConfigParser
这个常见于.conf,.ini等类型的配置文件 下面先看一下如果通过python生成一个.ini文件 import configparser #2.x is ConfigParserconfig = ...
- python之读取配置文件模块configparser(一)基本操作
configparser模块是读取类ini文件使用,其有固定的读取格式如下: [section1] option11 = value11 option12 = value12 .... [sectio ...
- python之读取配置文件模块configparser(二)参数详解
configparser.ConfigParser参数详解 从configparser的__ini__中可以看到有如下参数: def __init__(self, defaults=None, dic ...
- python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析
非标准配置文件也是经常使用的,如何使用configparser来解析? 这要从configparser本身解析结构来说,configparser包含section和option,非标准配置文件只有op ...
- python 读取配置文件ini ---ConfigParser
Python读取ini文件需要用到 ConfigParser 模块 关于ConfigParser模块的介绍详情请参照官网解释:https://docs.python.org/2.7/library/c ...
- Python读写配置文件模块--Configobj
一.介绍 我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某些信息时,可以直接修改这个配置文件,重启服务即可,不用再去 ...
- Python(2.7.6) ConfigParser - 读写配置文件
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...
随机推荐
- Memcached 笔记与总结(4)memcache 扩展的使用
在 wamp 环境下进行测试:WAMPSERVER 2.2(Windows 7 + Apache 2.2.21 + PHP 5.3.10 + memcache 3.0.8 + Memcached 1. ...
- UITableview cell 的多选
利用NSMutableDictionary key值 来改变cell的状态 -(void)createUI{ table = [[UITableView alloc]initWithFrame:CG ...
- C++类型转化分析(1)
仔细想想地位卑贱的类型转换功能(cast),其在程序设计中的地位就象goto语句一样令人鄙视.但是它还不是无法令人忍受,因为当在某些紧要的关头,类型转换还是必需的,这时它是一个必需品. 不过C风格的类 ...
- SVN使用安装
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- Two's complement
https://en.wikipedia.org/wiki/Two's_complement The two's-complement system has the advantage that th ...
- 【转】Unity3D的输入(Input)——键盘和鼠标
http://blog.csdn.net/lingyun_blog/article/details/41451565 Unity3D使用input类控制用户的输入,输入包括了用户键盘,鼠标,触摸,重力 ...
- forms6 builder安装之后设置注册表开发环境
- 单选按钮控件(Ridio Button)的使用
VC学习笔记5:单选按钮控件(Ridio Button)的使用 一.对单选按钮进行分组: 每组的第一个单选按钮设置属性:Group,Tabstop,Auto;其余按钮设置属性Tabstop,Auto. ...
- 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...
- JS事件分析
1.注册事件 1.1 使用HTML元素的事件属性 <div id='myDiv' style="width:100px;height:100px;background-color:re ...