Python中ConfigParser模块应用

Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser。ConfigParser和SafeConfigParser。当中RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。

以下看看如何通过ConfigParser类来解析一个ini文件。

配置文件settings.cfg

[DEFAULT]
mykey=myvalue [section_a]
key1=value1
key2=value2
key3=value3 [section_b]
key1=value1
key2=value2
key3=value3 [db]
db_host=127.0.0.1
db_port=5432
db_user=admin
db_pass=Letmein
db_name=test
db_url = jdbc:postgresql://%(db_host)s:%(db_port)s/%(db_name)s

測试代码例如以下

#coding:utf-8
import ConfigParser, os # 读取配置文件
cp = ConfigParser.ConfigParser()
cp.read(['settings.cfg']) # 获取全部defaults section
defaults = cp.defaults()
print defaults # 获取全部sections
sections = cp.sections()
print sections # 推断指定 section 是否存在
has_db = cp.has_section('db')
print has_db # 获取指定 section 的配置信息。仅仅获取键
options = cp.options('db')
print options # 获取指定 section 的配置信息。获取键值
items = cp.items('db')
print items # 获取指定 section 的配置信息。依据键获取值
db_host=cp.get('db', 'db_host')
db_port=cp.getint('db', 'db_port')
db_user=cp.get('db', 'db_user')
db_pass=cp.get('db', 'db_pass')
db_name=cp.get('db', 'db_name')
db_url=cp.get('db', 'db_url')
print db_host, db_port, db_name, db_user, db_pass # 使用 DEFAULT section 值
myvalue=cp.get('db', 'mykey')
print myvalue # 加入一个section
cp.add_section('section_c')
cp.set('section_c', 'key1', 'value111')
cp.set('section_c', 'key2', 'value222')
cp.set('section_c', 'key3', 'value333')
cp.set('section_c', 'key4', 'value444')
cp.write(open("test1.cfg", "w")) # 移除 option
cp.remove_option('section_c','key4') # 移除 section
cp.remove_section('section_c') cp.write(open("test2.cfg", "w"))

转载请以链接形式标明本文地址

本文地址:http://blog.csdn.net/kongxx/article/details/50449163

Python中ConfigParser模块应用的更多相关文章

  1. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  2. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  3. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

  4. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  5. python中configparser模块记录

    python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...

  6. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  7. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  8. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  9. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

随机推荐

  1. 【HIHOCODER 1067】最近公共祖先·二(LCA)

    描述 上上回说到,小Hi和小Ho用非常拙劣--或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中辈分最低的一个是谁.远在美国的他们利用了一些奇妙的技术获得了国内许多人的 ...

  2. PAT Basic 1010

    1010 一元多项式求导 设计函数求一元多项式的导数.(注:x^n^(n为整数)的一阶导数为n*x^n-1^.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数). ...

  3. WEB框架——WEB框架本质

    武sir http://www.cnblogs.com/wupeiqi/articles/5237672.html

  4. CSS 如何让 height:100%; 起作用

    当你设置一个页面元素的高度(height)为100%时,期望这样元素能撑满整个浏览器窗口的高度,但大多数情况下,这样的做法没有任何效果.你知道为什么height:100%不起作用吗? 按常理,当我们用 ...

  5. [转]python 多线程threading简单分析

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  6. PTA 09-排序1 排序 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/720 5-12 排序   (25分) 给定NN个(长整型范围内的)整数,要求输出从小到大 ...

  7. 九度oj 题目1534:数组中第K小的数字

    题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6]. 现在给你数组A和B ...

  8. WPF Custom Command And Binding

    using System; using System.Collections.Generic; using System.Windows.Input; namespace WPF.Commands { ...

  9. 跟着xiaoxin巨巨做cf

    cf 385 C. Bear and Prime Numbers 题目大意:有一个数列{xi},每次给出一个询问[l, r],即问 S(l ,r)是l和r之间的素数,f(p)表示数列{xi}中整除p的 ...

  10. 转 CListCtrl::InsertColumn、InsertItem、SetItemText;

    将数据写入到CListCtrl 向CListCtrl中写入数据,一般使用3个成员方法: CListCtrl::InsertColumn; CListCtrl::InsertItem; CListCtr ...