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数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
随机推荐
- uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)
option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...
- Python 构造函数、 Python 析构函数、Python 垃圾回收机制
构造函数与析构函数 构造函数: 用于初始化类的内容部状态,Python提供的构造函数式 __init__(); 也就是当该类被实例化的时候就会执行该函数.那么我们就可以把要先初始化的属性放到这个函数里 ...
- ViewPager滑动引导页
ViewPager实现Animation动画引导页 http://blog.csdn.net/ye_scofield/article/details/44831357 SurfaceView实现动 ...
- AtomicInteger保证线程安全的全局变量
现有业务场景需要做一个线程间的全局变量,并且实现自增效果. 初始使用了volatile 来保证count的安全性,如下: import java.util.concurrent.ExecutorSer ...
- 【BZOJ4621】Tc605 DP
[BZOJ4621]Tc605 Description 最初你有一个长度为 N 的数字序列 A.为了方便起见,序列 A 是一个排列. 你可以操作最多 K 次.每一次操作你可以先选定一个 A 的一个子串 ...
- 《从零开始学Swift》学习笔记(Day 63)——Cocoa Touch设计模式及应用之单例模式
原创文章,欢迎转载.转载请注明:关东升的博客 什么是设计模式.设计模式是在特定场景下对特定问题的解决方案,这些解决方案是经过反复论证和测试总结出来的.实际上,除了软件设计,设计模式也被广泛应用于其他领 ...
- delphi xe学习随意记录
学习来源(根据他们的资料整理) 论坛:http://www.coder163.com(有视频) 博客:http://del.cnblogs.com/(万一的博客) 1.1.1 命名规范的概述 1 ...
- Connecting to Shares And Common Shares
Now that you have these shares, how do people use them? Assuming that you have a share called Apps o ...
- info 手册
info flex 可以查看flex帮助. h就可以看到相关命令,常用命令已经加粗: x 关闭此帮助窗口. q 一并退出 Info. RET ...
- MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%) MylSAM平均每秒Key Buffer读命中率(%) MylSAM平均每秒Key Buffer写命中率(%)
MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%)MylSAM平均每秒Key Buffer读命中率(%)MylSAM平均每秒Key Buff ...