Python configparser模块常用方法解析

作者:疯了的小蜗 时间:2021-07-08 17:31:11 

ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。

注意:在python 3 中ConfigParser模块名已更名为configparser

configparser函数常用方法:

读取配置文件:

read(filename) #读取配置文件,直接读取ini文件内容

sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql']

options(sections) #获取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password']

items(sections) #获取指定section下所有的键值对,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]

get(section, option) #获取section中option的值,返回为string类型
>>>>>获取指定的section下的option <class 'str'> 127.0.0.1

getint(section,option) 返回int类型
getfloat(section, option) 返回float类型
getboolean(section,option) 返回boolen类型

举例如下:

配置文件ini如下:

[logging]
level = 20
path =
server =

[mysql]
host=127.0.0.1
port=3306
user=root
password=123456

注意,也可以使用:替换=

代码如下:


import configparser
from until.file_system import get_init_path

conf = configparser.ConfigParser()
file_path = get_init_path()
print('file_path :',file_path)
conf.read(file_path)

sections = conf.sections()
print('获取配置文件所有的section', sections)

options = conf.options('mysql')
print('获取指定section下所有option', options)

items = conf.items('mysql')
print('获取指定section下所有的键值对', items)

value = conf.get('mysql', 'host')
print('获取指定的section下的option', type(value), value)

运行结果如下:

file_path : /Users/xxx/Desktop/xxx/xxx/xxx.ini
获取配置文件所有的section ['logging', 'mysql']
获取指定section下所有option ['host', 'port', 'user', 'password']
获取指定section下所有的键值对 [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
获取指定的section下的option <class 'str'> 127.0.0.1

综合使用方法:


import configparser
"""
读取配置文件信息
"""

class ConfigParser():

config_dic = {}
 @classmethod
 def get_config(cls, sector, item):
   value = None
   try:
     value = cls.config_dic[sector][item]
   except KeyError:
     cf = configparser.ConfigParser()
     cf.read('settings.ini', encoding='utf8') #注意setting.ini配置文件的路径
     value = cf.get(sector, item)
     cls.config_dic = value
   finally:
     return value

if __name__ == '__main__':
 con = ConfigParser()
 res = con.get_config('logging', 'level')
 print(res)

来源:https://www.cnblogs.com/insane-Mr-Li/p/9952827.html

标签:Python,configparser,模块
0
投稿

猜你喜欢

  • 网马解密大讲堂——网马解密中级篇(Freshow工具使用方法)

    2009-09-16 15:09:00
  • 显示你个性的鼠标指针

    2011-06-14 09:41:41
  • ACCESS 2007出现“错误 '80040e14'“

    2008-06-19 13:21:00
  • php利用ob_start()清除输出和选择性输出的方法

    2023-11-18 08:54:35
  • SQL Server数据库查询优化3种技巧

    2008-10-17 10:10:00
  • 使用MySQL数据库的23个注意事项

    2010-03-18 15:46:00
  • Python3 解释器的实现

    2023-08-09 17:08:53
  • go语言之给定英语文章统计单词数量(go语言小练习)

    2023-09-16 09:11:12
  • 三种禁用FileSystemObject组件的方法

    2007-09-23 15:52:00
  • SQL Server连接中经常出现的3个常见错误与解答

    2010-07-26 14:25:00
  • 一道求$b相对于$a的相对路径的php代码

    2023-07-12 05:37:46
  • js 数值项目的格式化函数代码

    2023-07-14 16:51:31
  • ASP+FSO+框架实现ASP生成htm并分页的方法(批量)

    2009-03-09 18:26:00
  • 编写一个JS组件来说说call和apply的用法

    2008-11-23 17:11:00
  • IE7 与 IE6 的模式窗口尺寸差异

    2008-03-06 13:49:00
  • 轻松接触SQL Server 2000实例的命名规则

    2009-01-23 13:44:00
  • python抓取网站的图片并下载到本地的方法

    2022-05-19 23:45:08
  • Oracle下时间转换在几种语言中的实现

    2009-02-28 11:09:00
  • php二分查找二种实现示例

    2023-11-21 00:40:13
  • IE6绝对定位的bug及其解决办法

    2011-03-30 12:31:00
  • asp之家 网络编程 m.aspxhome.com