写项目肯定用的到配置文件,这次学习一下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的更多相关文章

  1. Python之配置模块ConfigParser

    http://docs.python.org/2/library/configparser.html http://www.cnblogs.com/sislcb/archive/2008/11/25/ ...

  2. Python解析配置文件模块:ConfigPhaser

    算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...

  3. python之常用模块ConfigParser

    这个常见于.conf,.ini等类型的配置文件 下面先看一下如果通过python生成一个.ini文件 import configparser #2.x is ConfigParserconfig = ...

  4. python之读取配置文件模块configparser(一)基本操作

    configparser模块是读取类ini文件使用,其有固定的读取格式如下: [section1] option11 = value11 option12 = value12 .... [sectio ...

  5. python之读取配置文件模块configparser(二)参数详解

    configparser.ConfigParser参数详解 从configparser的__ini__中可以看到有如下参数: def __init__(self, defaults=None, dic ...

  6. python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析

    非标准配置文件也是经常使用的,如何使用configparser来解析? 这要从configparser本身解析结构来说,configparser包含section和option,非标准配置文件只有op ...

  7. python 读取配置文件ini ---ConfigParser

    Python读取ini文件需要用到 ConfigParser 模块 关于ConfigParser模块的介绍详情请参照官网解释:https://docs.python.org/2.7/library/c ...

  8. Python读写配置文件模块--Configobj

    一.介绍 我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某些信息时,可以直接修改这个配置文件,重启服务即可,不用再去 ...

  9. Python(2.7.6) ConfigParser - 读写配置文件

    Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...

随机推荐

  1. 2016.03.31,英语,《Vocabulary Builder》Unit 08

    tend/tent: from the Latin tendere, meaning 'to stretch, extend, or spread'. tent: [tent] n. 帐篷 vt.&a ...

  2. Linux 计划任务 Crontab 笔记与总结(1)

    Linux 版本:CentOS 6.6 应用场景,例如: ① 每分钟执行一个程序检查系统运行状态 ② 每天凌晨需要对过去一天的业务数据进行统计 ③ 每个星期需要把日志文件备份 ④ 每个月把数据库进行备 ...

  3. STM32全球唯一ID读取方法

    产品唯一的身份标识非常适合:● 用来作为序列号(例如USB字符序列号或者其他的终端应用)● 用来作为密码,在编写闪存时,将此唯一标识与软件加解密算法结合使用,提高代码在闪存存储器内的安全性.● 用来激 ...

  4. Amoeba基本配置

    Amoeba安装及读写分离配置一.amoeba简介官网:http://docs.hexnova.com/amoeba/index.html二.Centos下安装jdk1.yum 安装1.6版本jdk2 ...

  5. 【转】Java 5种字符串拼接方式性能比较。

    最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...

  6. 【重要】ASCII码表

    我们在做业务项目,客户端的输入总是无法控制,有各种各样的特殊字符,这些特殊字符就要借助ASCII码表才能判断,所以我做了一张图,方便查看 为什么要搞个表出来,下面的字符串中,你看看你能否看的出来是什么 ...

  7. Ubuntu 14.04 在桌面上双击运行shell 脚本文件

    http://askubuntu.com/questions/465531/how-to-make-a-shell-file-execute-by-double-click up vote7down ...

  8. dubbo,gradle,spring build from source

    https://github.com/alibaba/dubbo http://www.csdn.net/article/2012-11-13/2811827-alibaba-Dubbo-Distri ...

  9. crossvcl.com - 用VCL开发MacOS软件

    还没正式发布.用力戳 http://crossvcl.com/ 消息来源:Delphi G+ Group.具体信息还没完全披露,但是可以肯定的是,不是通过FireMonkey,而是通过原生的MacOS ...

  10. 【Android测试】【随笔】搜狗、腾讯技术交流会心得

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5048525.html 今天没上班,一大早就起来赶去搜狐网络 ...