对python 读写配置文件的具体方案的介绍

1,函数介绍


import configParser 如果Configparser无效将导入的configParser 的C小写

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

配置文件test.cfg
[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

 
 以前不会排版,最近才学会排版,看着舒服多了
 
--------------------------------------------------------------------------------------------------------
来源:http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944

python-ConfigParser模块【读写配置文件】的更多相关文章

  1. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  2. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  3. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  4. 用ConfigParser模块读写配置文件——Python

    对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...

  5. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

  6. configparser模块读写ini配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  7. python ConfigParser模块 配置文件解析

    ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...

  8. Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...

  9. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  10. python -ConfigParser模块讲解

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

随机推荐

  1. java == 与 equals 相同与不同点

    java中与很多有意思又值得深究的点. 写这篇文章呢,是由于在百度知道中看到一个问题:怎样比较两个对象是否相同.这又使我想到了另外一个问题,== 和 equals有什么不同?写了几行代码,看了几篇文章 ...

  2. Qt浅谈之一:内存泄露(总结)

    一.简介       Qt内存管理机制:Qt 在内部能够维护对象的层次结构.对于可视元素,这种层次结构就是子组件与父组件的关系:对于非可视元素,则是一个对象与另一个对象的从属关系.在 Qt 中,在 Q ...

  3. python--元组tuple

    元组与列表一样,都是序列.但元组不能修改内容(列表允许) 默认的,元组通过圆括号括起来 1. 使用type函数查看类型 numbers = (1,2,3,4,5,6,7,8,9,0) print(ty ...

  4. 使用tableau去将存入mysql都地区点击率进行了展示 感觉很好用

    1.连接数据源很多选项:hive mysql Oracle 等所有数据库 2.写上hive2的那个客户端连接,下边会显示出让我装连接的驱动 ,所有jdbc都需要这样 点进去找到windows的下载一键 ...

  5. C++ 开源库列表

    https://zh.cppreference.com/w/cpp/links/libs

  6. MySQL笔记(八)存储过程练习&补充

    存储过程有什么优缺点?为什么要用存储过程?或者在什么情况下才用存储过程? 最直白的好处是存储过程比较快. 1.利用存储过程,给Employee表添加一条业务部门员工的信息. DROP PROCEDUR ...

  7. jackson JsonPropertyOrder和@JsonIgnoreProperties注解

    有些时候,我们在和外部系统交互的时候使用了json作为标准的数据交换格式,同时为了安全性考虑,增加了对报文的校验,因此我们需要确保序列化的时候参数有序且不多不少刚好,因为对外的API不像后台和前端交互 ...

  8. C#实现根据地图上的两点坐标,计算直线距离

    根据地图上的两点坐标,计算直线距离,在网上找到javascript的写法,用C#实现一下 /// <summary> /// 根据地图上的两点坐标,计算直线距离 /// </summ ...

  9. 寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)

    一.问题提出 这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-hol ...

  10. Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划

    In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlop ...