python中configparser模块
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模块的更多相关文章
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- 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中有两个内 ...
随机推荐
- 【算法杂谈】Miller-Rabin素性测试算法
额,我们今天来讲一讲Miller-Rabin素性测试算法. 读者:怎么又是随机算法!!!(⊙o⊙)… [好了,言归正传] [费马小定理] 费马小定理只是个必要条件,符合费马小定理而非素数的数叫做Car ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- oracle EXP导出一张表时使用query参数指定where条件
oracle exp 导出一个表的部分内容,使用query参数可加上SQL的where条件进行过滤 注意:如果需要使用到日期字符串格式等单引号,需要使用双引号将where条件括起来,而且双引号要用\做 ...
- js兼容总结之获取非行间样式
非行间样式案例 #div1 { width: 200px; height: 200px; background: red; } IE获取非行间样式 var oDiv = document.getEle ...
- linux工具
sudo yum install yum-utils
- 格式化input输入内容(金额)
项目中要用到格式化金额输入框,要求每三个数字用逗号分割开. 添加一个directive angular.module('myApp.directives', []) .directive('filte ...
- iOS开发之记录用户登录状态
iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...
- JavaWeb中读取文件资源的路径问题
在做javaweb开发的时候,我们可能会需要从本地硬盘上读取某一个文件资源,或者修改某一个文件,这个时候就需要先找到这个文件,然后用FileInputStrem等文件字节.字符流来将这个文件读取到内存 ...
- perl学习之路2
这些主要是从 "小骆驼" 书上粘贴或者摘抄出来的, 个人认为需要记的语法知识 "在某些情况下, 你可能需要在一台机器上写程序, 再传送到另一台机器上运行.这时候, 请使用 ...
- nginx 各类网站设置 (laravel , thinkphp , nodejs , https)
基础部分设置 [root@centos ~]# vim /opt/nginx/conf/nginx.conf user www www;worker_processes auto;pid logs/n ...