python中的configparse模块的使用

主要用来解析一些常用的配置,比如数据配置等。

例如:有一个dbconfig.ini的文件

 [section_db1]
db = test_db1
host = 127.0.0.1
port = 3319
user = root
password = 123456 [section_db2]
db = test_db2
host = 127.0.0.1
port = 3318
user = root
password = 123456 [section_db5]
db = test_db5

常用操作代码:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Eric.yue import configparser
conf = configparser.ConfigParser() '''
读取操作
'''
#获取section
conf.read('dbconfig.ini')
ret = conf.sections()
print ret
#[u'section_db1', u'section_db2'] #指定节点下所有的键
ret = conf.options('section_db1')
print ret #[u'db', u'host', u'port', u'user', u'password'] #获取指定的键值对
ret = conf.items('section_db1')
print ret
#[(u'db', u'test_db1'), (u'host', u'127.0.0.1'), (u'port', u'3319'), (u'user', u'root'), (u'password', u'123456')] conf.read('dbconfig.ini') #文件路径
dbname = conf.get("section_db1","db") #获取指定section的db
print dbname
port = conf.getint("section_db1","port") #获取指定section的port print type(port)
#<type 'int'> #判断节点是否存在
has_sec = conf.has_section('section_db2')
print has_sec #True # 添加节点
#conf.add_section("section_db4")
#conf.set("section_db4", "db", "test_db4") #conf.add_section("section_db5")
#conf.set("section_db5", "db", "test_db5")
#conf.write(open('dbconfig.ini', 'w')) #删除节点,会把对应节点下面的东西都会删除掉
conf.remove_section("section_db4")
conf.write(open('dbconfig.ini', 'w')) '''
写入配置文件操作
'''
'''
#修改
conf.read('dbconfig.ini') #文件路径
conf.set("section_db1", "db", "test_db1") #增加指定section 的option的db
conf.set("section_db1", "port", "3319") #增加指定section 的option的port #增加
conf.add_section("section_db3") #增加section
conf.set("section_db3", "db", "test_db3")
conf.set("section_db3", "host", "127.0.0.1")
conf.set("section_db3", "port", "3319")
conf.set("section_db3", "user", "root") #最后执行才操作
conf.write(open('dbconfig.ini', 'w'))
'''

python中configparser模块的更多相关文章

  1. python中confIgparser模块学习

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

  2. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

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

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

  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. Gulp常用前端流程自动化配置

    前言 近期的项目全部由Grunt + LESS 转向改用Gulp + SASS 进行前端开发,也就奔着Gulp那比较好用的自定义函数而来的. 一.package.json文件配置如下: { " ...

  2. python 引用传递与值传递

    https://taizilongxu.gitbooks.io/stackoverflow-about-python/content/16/README.html 1.也就是如果传可变对象,就是引用传 ...

  3. 2.1 -1.0 Xcode(发布时间、使用、快捷键、插件相关)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 1.0 Xcode 发布时间 版本 iOS 版本 手机 日期 特殊介绍 Xcode 3.1 ...

  4. dotnet webapi 中添加Swagger文档

    首先添加"SwaggerGenerator": "1.1.0","SwaggerUi": "1.1.0" 需要注意的是这 ...

  5. 【PostgreSQL】PostgreSQL的安装

    到了新公司,新公司的数据库是使用PostgreSQL,第一次学习,第一次安装. 开始安装:

  6. The Practical Guide to Empathy Maps: 10-Minute User Personas

    That’s where the empathy map comes in. When created correctly, empathy maps serve as the perfect lea ...

  7. Python 爬虫6——Scrapy的安装和使用

    前面我们简述了使用Python自带的urllib和urllib2库完成的一下爬取网页数据的操作,但其实能完成的功能都很简单,假如要进行复制的数据匹配和高效的操作,可以引入第三方的框架,例如Scrapy ...

  8. 用SQL Server(T-SQL)获取连接字符串

    一般情况下,C# 连接SQL Server的字符串可以直接按照说明文档直接手动写出来,或者也可以参考大名鼎鼎的connectionstrings手动拼写 但是如果你已经连接到SQL Server也可以 ...

  9. jQuery extend扩展String原型

    jQuery.extend(String.prototype, { isPositiveInteger:function(){ return (new RegExp(/^[1-9]\d*$/).tes ...

  10. java-如何用eclipse打包jar

    Eclipse通过导出的方式(右键单击项目,之后选择Export)打包java类文件生成jar包. 方法一:(在项目工程没有引用外部jar包时,直接导出) 选中工程---->右键,Export. ...