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 ...
随机推荐
- AMD GPU spec (public)
http://www.x.org/docs/AMD/old/ Index of /docs/AMD/old Name Last modified Size Description Parent Dir ...
- Bootstrap页面布局24 - BS旋转木马功能
代码: <div class='container-fluid'> <h3 class='page-header'>Bootstrap 旋转木马</h3> < ...
- image hover
http://www.nxworld.net/tips/css-image-hover-effects.html
- How to Write a Spelling Corrector
http://norvig.com/spell-correct.html Feb 2007to August 2016 How to Write a Spelling Corrector One we ...
- link them together by means of pointers
Computer Science An Overview _J. Glenn Brookshear _11th Edition An alternative to storing a heteroge ...
- Git相关的项目
1.posh-git Git的PowerShell扩展 项目地址: https://github.com/dahlbyk/posh-git 可以用psget快速安装扩展模块,psget下载安装地址 h ...
- JS 去字符串空格 总结
str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...
- HBase的属性
一:基本属性 1.查看属性 2.解释属性 NAME:列簇名 BLOOMFILTER:布隆过滤器,用于对storefile的过滤 共有三种类型: ROW:行健过滤 ROWCOL:行列过滤 NONE:无 ...
- CSS动画控制器
<html> <head> <title>animation</title> <style> div{ width: 100px; heig ...
- backbone extend 源码分析
var extend = function(protoProps, staticProps) { var parent = this; var child; if (protoProps && ...