python-ConfigParser模块--转载
1,函数介绍
1.1.读取配置文件
-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型
1.2.写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
需要调用write将内容写入配置文件。
2,测试实例
2.1,测试1
配置文件test.cfg
- [sec_a]
- a_key1 = 20
- a_key2 = 10
- [sec_b]
- b_key1 = 121
- b_key2 = b_value2
- b_key3 = $r
- b_key4 = 127.0.0.1
测试文件test.py
- # -* - coding: UTF-8 -* -
- import ConfigParser
- #生成config对象
- conf = ConfigParser.ConfigParser()
- #用config对象读取配置文件
- conf.read("test.cfg")
- #以列表形式返回所有的section
- sections = conf.sections()
- print 'sections:', sections #sections: ['sec_b', 'sec_a']
- #得到指定section的所有option
- options = conf.options("sec_a")
- print 'options:', options #options: ['a_key1', 'a_key2']
- #得到指定section的所有键值对
- kvs = conf.items("sec_a")
- print 'sec_a:', kvs #sec_a: [('a_key1', '20'), ('a_key2', '10')]
- #指定section,option读取值
- str_val = conf.get("sec_a", "a_key1")
- int_val = conf.getint("sec_a", "a_key2")
- print "value for sec_a's a_key1:", str_val #value for sec_a's a_key1: 20
- print "value for sec_a's a_key2:", int_val #value for sec_a's a_key2: 10
- #写配置文件
- #更新指定section,option的值
- conf.set("sec_b", "b_key3", "new-$r")
- #写入指定section增加新option和值
- conf.set("sec_b", "b_newkey", "new-value")
- #增加新的section
- conf.add_section('a_new_section')
- conf.set('a_new_section', 'new_key', 'new_value')
- #写回配置文件
- conf.write(open("test.cfg", "w"))
2.2,测试2
- [info]
- age = 21
- name = chen
- sex = male
测试文件test.py
- from __future__ import with_statement
- import ConfigParser
- config=ConfigParser.ConfigParser()
- with open("test.cfg","rw") as cfgfile:
- config.readfp(cfgfile)
- name=config.get("info","name")
- age=config.get("info","age")
- print name
- print age
- config.set("info","sex","male")
- config.set("info","age","55")
- age=config.getint("info","age")
- print name
- print type(age)
- print age
分析
其中[ ] 中的info是这段配置的名字。
其中age,name都是属性。
首先,config=ConfigParser.ConfigParser() 得到一个配置config对象.下面打开一个配置文件 cfgfile. 用readfp()读取这个文件.这样配置的内容就读到config对象里面了。
接下来一个问题是如何读取值.常用的方法是get() 和getint() . get()返回文本. getint()返回整数。
其次,name=config.get(''info'',''name'') 意思就是.读取config中info段中的name变量值。
最后讲讲如何设置值.使用set(段名,变量名,值) 来设置变量.config.set(''info'',''age'',''21'') 表示把info段中age变量设置为21。
2.3,测试3
Python的ConfigParser Module中定义了3个类对INI文件进行操作。
分别是RawConfigParser、ConfigParser、SafeConfigParser。
RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。
配置文件test.cfg
- [portal]
- url = http://%(host)s:%(port)s/Portal
- host = localhost
- port = 8080
使用RawConfigParser:
- import ConfigParser
- conf = ConfigParser.RawConfigParser()
- print "use RawConfigParser() read"
- conf.read("test.cfg")
- print conf.get("portal", "url")
- print "use RawConfigParser() write"
- conf.set("portal", "url2", "%(host)s:%(port)s")
- print conf.get("portal", "url2")
得到输出
- use RawConfigParser() read
- http://%(host)s:%(port)s/Portal
- use RawConfigParser() write
- %(host)s:%(port)s
改用ConfigParser
- import ConfigParser
- conf = ConfigParser.ConfigParser()
- print "use RawConfigParser() read"
- conf.read("test.cfg")
- print conf.get("portal", "url")
- print "use RawConfigParser() write"
- conf.set("portal", "url2", "%(host)s:%(port)s")
- print conf.get("portal", "url2")
得到输出
- use RawConfigParser() read
- http://localhost:8080/Portal
- use RawConfigParser() write
- localhost:8080
改用SafeConfigParser,效果与ConfigParser相同
- import ConfigParser
- conf = ConfigParser.SafeConfigParser()
- print "use RawConfigParser() read"
- conf.read("test.cfg")
- print conf.get("portal", "url")
- print "use RawConfigParser() write"
- conf.set("portal", "url2", "%(host)s:%(port)s")
- print conf.get("portal", "url2")
结论:
还是用ConfigParser
python-ConfigParser模块--转载的更多相关文章
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- python -ConfigParser模块讲解
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- python ConfigParser模块 配置文件解析
ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...
- 【python】python configparser模块
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Python - configParser模块学习
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- 聊聊Python ctypes 模块(转载)
https://zhuanlan.zhihu.com/p/20152309?columnSlug=python-dev 作者:Jerry Jho链接:https://zhuanlan.zhihu.co ...
- python ConfigParser 模块
ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...
- python configparser模块
来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...
- Python configparser模块操作代码实例
1.生成配置文件 ''' 生成配置文件 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知 ...
随机推荐
- matplotlib —— 调整坐标轴
import matplotlib.pyplot as plt import numpy as np # 绘制普通图像 x = np.linspace(-1, 1, 50) y1 = 2 * x + ...
- vue项目优化
cross-env 包环境 静态文件分离 require 是置顶的 双斜杠 //baidu.com可以是http也可以是https require.ensure打包到不同的文件中 项目文件路径规范 ...
- JS 数组和对象的遍历方式,以及几种方式的比较。
通常我们会用循环的方式来遍历数组.但是循环是 导致js 性能问题的原因之一.一般我们会采用下几种方式来进行数组的遍历: 方式1: for in 循环: var arr = [1,2,3,4,5]; v ...
- unity3d-碰撞检测
碰撞检测 游戏中很多时候都要判断碰撞检测,比如子弹打中敌机.当碰撞后.就要发生爆炸. 或者敌机减血, 我们先看一张图片,看皮球从天空下落.与地面碰撞的过程 碰撞检测条件 游戏中两个对象发生碰撞是需要条 ...
- 第七章之main函数和启动例程
main函数和启动例程 为什么汇编程序的入口是_start,而C程序的入口是main函数呢?本节就来解释这个问题.在讲例 18.1 “最简单的汇编程序”时,我们的汇编和链接步骤是: $ as hell ...
- discuz $_G变量
class.core.php中 global $_G; $_G = array( 'uid' => 0, 'username' => ...
- html5<embed>的完整属性
问题起因:网页中插入Flash,看了代码,然后呢,小小的学习下 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000& ...
- Python: 列表推导式--轻量级循环
定义: 列表推导式(list comprehension)是利用其他列表创建新列表的一种方法,其工作方式类似于for循环,对列表进行过滤变种操作 eg1: >>> [x*x for ...
- linux常用命令:whereis 命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- 最新版Intellij IDEA插件JRebel 7.0.7官方免费激活
本文转自:http://blog.csdn.net/u012283609/article/details/70213307 开场语 有时候真实比小说更加荒诞,因为虚构是在一定逻辑下进行的,而现实往往毫 ...