Python3.x:ConfigParser模块的使用
Python3.x:ConfigParser模块的使用
简介
ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini
配置文件相似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
使用的配置文件的好处就是不用再程序中硬编码,可以是你的程序变得灵活起来。
注意:在python 3 中ConfigParser
模块名已更名为configparser
安装
pip install ConfigParser
函数
1,读取配置文件
(1)read(filename) 直接读取ini文件内容
(2)sections() 得到所有的section,并以列表的形式返回
(3)options(section) 得到该section的所有option
(4)items(section) 得到该section的所有键值对
(5)get(section,option) 得到section中option的值,返回为string类型
(6)getint(section,option) 得到section中option的值,返回为int类型
(7)getfloat(section,option)得到section中option的值,返回为float类型
(8)getboolean(section, option)得到section中option的值,返回为boolean类型 2,写入配置文件
(1)add_section(section) 添加一个新的section
(2)has_section(section) 判断是否有section
(3)set( section, option, value) 对section中的option进行设置
(4)remove_setion(section)删除一个section
(5)remove_option(section, option)删除section中的option
(6)write(fileobject)将内容写入配置文件。
属性文件格式(dbconfig.ini)
#数据库参数
[dbserver]
ip=192.20.101.100
port=3306
user=root
password=123456
dbname=mydb
注意:也可以使用:
替换=
代码
#python3
#author:lizm
#date:2018-01-31
'''
demo:ConfigParser使用
'''
import configparser
import sys #获取属性文件的值
def dbconfig():
#生成config对象
cfg = configparser.ConfigParser()
#用config对象读取配置文件
cfg.read("dbconfig.ini")
#以列表形式返回所有的section
sections = cfg.sections()
print('sections:', sections)
#输出:sections: ['dbserver']
#得到指定section的所有option
options = cfg.options("dbserver")
print('options:', options)
#输出:options: ['ip', 'port', 'user', 'password', 'dbname']
#得到指定section的所有键值对
useritem = (cfg.items("dbserver"))
print('user:', useritem)
#输出:user: [('ip', '192.20.101.100'), ('port', '3306'), ('user', 'root'), ('password', '123456'), ('dbname', 'mydb')]
#指定section,option读取值
ip = cfg.get("dbserver", "ip")
port = cfg.get("dbserver", "port")
user = cfg.get("dbserver", "user")
password = cfg.get("dbserver", "password")
dbname = cfg.get("dbserver", "dbname")
return (ip,port,user,password,dbname) def writeConfig():
cfg = configparser.ConfigParser()
#用config对象读取配置文件
cfg.read("dbconfig.ini")
#更新指定section,option的值
cfg.set("dbserver", "ip", "192.25.103.150")
#写入指定section增加新option和值
cfg.set("dbserver", "tablenmae", "py_table")
#增加新的section
cfg.add_section('dbserver2')
cfg.set('dbserver2', 'ip', '192.25.105.100')
#写回配置文件
cfg.write(open("dbconfig.ini", "w")) if __name__ == '__main__':
dbconfig = dbconfig()
vrg_ip=dbconfig[0]
vrg_port=dbconfig[1]
vrg_user=dbconfig[2]
vrg_password=dbconfig[3]
vrg_dbname=dbconfig[4]
writeConfig()
注意:用python命令执行测试py:
方法一:切换到py所在的目录再执行python命令,正常
方法二:不切换目录直接执行python命令,异常:属性文件的[dbserver]不存在
原因:没有切到py所在的目录,没有找到正确的属性文件,导致异常;
解决方案:
#用config对象读取配置文件
cfg.read("dbconfig.ini") #改为 #用config对象读取配置文件
#获取当前文件的目录
path_ = sys.path[0]
cfg.read(path_+"\dbconfig.ini")
作者:整合侠
链接:http://www.cnblogs.com/lizm166/p/8392141.html
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Python3.x:ConfigParser模块的使用的更多相关文章
- Python3之configparser模块
1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...
- (15)-Python3之--configparser模块
1.模块简介 configparser模块是python用来读取配置文件的模块,置文件的格式跟windows下的ini或conf配置文件相似,可以包含一个或多个节(section), 每个节可以有多个 ...
- python3 之configparser 模块
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近[db]db_count = 31 = passwd2 = dat ...
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- Python3 中 configparser 模块用法
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- (转)python的ConfigParser模块
原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...
- 【python3】configparser读取ini配置文件
在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...
- Python3 logging模块&ConfigParser模块
''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
随机推荐
- cobbler 修改 distro_signatures.json
edit file in /var/lib/cobbler/distro_signatures.json and restart cobblerd service 转自: https://lists. ...
- 《The Swift Programming Language》的笔记-第24页
The Swift Programming Language读书笔记学习笔记 第24页 本页主要内容有两个:打印输出和怎样在swift凝视代码 1 怎样打印变量和常量的值? 使用println函数,细 ...
- db2 clob dbclob
DB2有三种类型的大字段: clob(Character Large OBjects ) 适用于存放单字节的字符串,当我们要保存的字符长度超过varchar的最大长度(32K)时,我们就要考虑使用cl ...
- 转载:resNet论文笔记
<Deep Residual Learning for Image Recognition>是2016年 kaiming大神CVPR的最佳论文 原文:http://m.blog.csdn. ...
- ilbc编解码在android实现
iLBC 是为专为提供稳健的 IP 语音通信而开发的语音 codec,以窄带语音为设计基础,具有 8 kHz 的采样率.iLBC codec 支持两种基本的帧长度:13.3 kbps 比特率下编码帧长 ...
- 巨蟒python全栈开发-第15天 装饰器
一.今日内容总览 关于函数的装饰器1.装饰器(重点,难点)(要求:反复写,代码不多但是很绕) 开闭原则:(比如,菜单是拆散的,一点点搞的,用友拆散自己的功能,以后就不用开发了) (1)对功能的扩展开放 ...
- Storm-源码分析-acker (backtype.storm.daemon.acker)
backtype.storm.daemon.acker 设计的巧妙在于, 不用分别记录和track, stream过程中所有的tuple, 而只需要track root tuple, 而所有中间过程都 ...
- GetWindowThreadProcessId
函数功能:该函数返回创建指定窗口线程的标识和创建窗口的进程的标识符,后一项是可选的. 函数原型:DWORD GetWindowThreadProcessld(HWND hwnd,LPDWORD lpd ...
- Python元组组成的列表转化为字典
虽然元组.列表不可以直接转化为字典,但下面的确是可行的,因为经常用python从数据库中读出的是元组形式的数据. # 原始数据 rows = (('apollo', 'male', '164.jpeg ...
- session补充