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模块的使用的更多相关文章

  1. Python3之configparser模块

    1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...

  2. (15)-Python3之--configparser模块

    1.模块简介 configparser模块是python用来读取配置文件的模块,置文件的格式跟windows下的ini或conf配置文件相似,可以包含一个或多个节(section), 每个节可以有多个 ...

  3. python3 之configparser 模块

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近[db]db_count = 31 = passwd2 = dat ...

  4. Python3 中 configparser 模块解析配置的用法详解

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  5. Python3 中 configparser 模块用法

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  6. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  7. 【python3】configparser读取ini配置文件

    在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...

  8. Python3 logging模块&ConfigParser模块

    ''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...

  9. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

随机推荐

  1. linux less使用方法

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  2. Android基站定位

    Android基站定位   一.通过手机信号获取基站信息 通过TelephonyManager 获取lac:mcc:mnc:cell-id(基站信息)的解释: MCC.Mobile Country C ...

  3. 【SVM】清晰明了的理论文章

    http://www.cnblogs.com/jerrylead/archive/2011/03/13/1982639.html 松弛变量和惩罚因子: http://blog.csdn.net/yan ...

  4. Json对象与Json字符串互转(4种转换方式) jquery 以及 js 的方式

    http://blog.csdn.net/zero_295813128/article/details/51545467

  5. commit和rollback

    COMMIT过程·已经在 SGA(系统全局区域)中产生了回滚段(Rollback segment)记录.·已经在 SGA 中产生了修改数据块.·已经在 SGA 中产生了上面两条的缓冲重做(redo). ...

  6. 下载xftp,xshell进行与linux端的远程操作

    在window下下载xftp5和xshell5 xshell主要是对远程的及其进行访问,在远程的情况下进行操作 xftp可以对远程的机器进行文件传输. 我安装这两个是单个的安装的. 进入官网 http ...

  7. sublime text 3 并列显示

    alt+shift+1:显示一列 alt+shift+2:显示二列 alt+shift+3:显示三列 ......

  8. 用vector构造自动扩容的二维数组

    #include <iostream> #include <string> #include <vector> using namespace std; int m ...

  9. 160727、自定义hibernate主键生成策略生成字符串+数字自增长

    需求:需要自增长注解如MyId0001.MyId0002.MyId0003 实现:实现这个接口org.hibernate.id.IdentifierGenerator 一.MyIdGenerator. ...

  10. 在java中public void与public static void有什么区别 ?

    public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...