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. Ubuntu18.04 无法解析域名

    解决方法: 首先先输入以下4条命令 1. sudo lshw -numeric -class network2. sudo ifconfig -a3. sudo route -nv4. sudo dh ...

  2. int main(int argc,char *argv[])的具体含义

    int main(int argc,char * argv[]) argv为指针的指针 argc为整数 char **argv or: char *argv[] or: char argv[][] m ...

  3. c++ heap学习

    heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制. 而这个实现机制中的max-hea ...

  4. luogu3809 后缀排序 后缀数组

    ref and 挑战程序设计竞赛. 主要是发现自己以前写得代码太难看而且忘光了,而且我字符串死活学不会啊,kmp这种东西我都觉得是省选+难度啊QAQ #include <iostream> ...

  5. Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \

    class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vect ...

  6. lamp环境配置,ubunutu下

    Ubuntu下快速搭建LAMP环境过程记录: 安装 Apache2: sudo apt-get install apache2 安装PHP模块: sudo apt-get install php5 安 ...

  7. ASP.NET(三):整体总结

    导读:经过一段时间的学习,我的ASP.NET也算是结束了.在这个过程中,总结了它的六大对象,现在先做个总体的总结,然后还会总结一下真假分页的情况.只有总结才能收获.ASP.net严格说起来,其实在VB ...

  8. 转自kuangbin的AC自动机(赛前最后一博)

    有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很 ...

  9. POJ 1745 Divisibility

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9476   Accepted: 3300 Desc ...

  10. Opencv学习笔记——视频进度条的随动

    1. CvCapture结构体: CvCapture是一个结构体,用来保存图像捕获的信息,就像一种数据类型(如int,char等)只是存放的内容不一样,在OpenCv中,它最大的作用就是处理视频时(程 ...