Python中ConfigParser模块应用
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模块应用的更多相关文章
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- python中configparser模块记录
python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- (转)python的ConfigParser模块
原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- Python中optionParser模块的使用方法[转]
本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...
随机推荐
- .net 操作Access数据库
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- bean容器能力
bean容器能力 对bean容器的最简单的述求 能生产bean 能注入组装bean: 字段,构造函数 spring bean容器(3.0版本)的能力列表 能生产bean 能注入组装bean:字段,构造 ...
- A. Light Bulb
A. Light Bulb Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO f ...
- 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
上一场CF打到心态爆炸,这几天也没啥想干的 A Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- rsync同步命令
rsync同步时,--delete删除目标目录比源目录多余文件的方法1 .实例说明 服务器A上同步/tmp/work目录到远程服务器B的/tmp/work目录下, 同时删除B服务器/tmp/work ...
- RDDs基本操作、RDDs特性、KeyValue对RDDs、RDD依赖
摘要:RDD是Spark中极为重要的数据抽象,这里总结RDD的概念,基本操作Transformation(转换)与Action,RDDs的特性,KeyValue对RDDs的Transformation ...
- 【Luogu】P1607庙会班车Fair Shuttle(线段树+贪心)
我不会做贪心题啊……贪心题啊……题啊……啊…… 我真TM菜爆了啊…… 这题就像凌乱的yyy一样,把终点排序,终点相同的按起点排序.然后维护一个查询最大值的线段树.对于一个区间[l,r],如果这个区间已 ...
- [POJ1143]Number Game
[POJ1143]Number Game 试题描述 Christine and Matt are playing an exciting game they just invented: the Nu ...
- 转载: LINK : fatal error LNK1104: 无法打开文件“mfc71.lib”的原因又一例
转载地址:http://blog.csdn.net/mxclxp/article/details/8196142 LINK : fatal error LNK1104: 无法打开文件“mfc71.li ...
- VMware 如何通过现有虚拟机克隆新的虚拟机 (图文)
本文做的是克隆主机,并重命名主机名,做好主机名与IP的对应关系,并可以通过主机名访问对方的主机. 首先说一下克隆虚拟机的作用 克隆虚拟机的作用 因工作需要,需要用到多个虚拟机环境时,再新建几个比较麻烦 ...