python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件

一、常用函数

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)

  综合使用方法:

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)

二、设置某个option 的值

conf.set("topsecret.server.com","port","67000") #修改port的值
conf.write(open("config_parser","w")) #u将修改的内容回写到文件上

 三、添加一个section

conf.add_section('liuqing')
conf.set('liuqing', 'int', '')
conf.set('liuqing', 'bool', 'true')
conf.set('liuqing', 'float', '3.1415')
conf.write(open("config_parser","w"))

四、

. 移除section 或者option 。(只要进行了修改就要写回的哦)

1 conf.remove__option('liuqing','int')
2 conf.remove_section('liuqing')
3 conf.write(open("test.conf", "w"))

python中configparser模块记录的更多相关文章

  1. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  2. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  3. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  4. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

  5. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  6. 【转】关于python中re模块split方法的使用

    注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...

  7. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  8. (转)python的ConfigParser模块

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

  9. Python中logging模块的基本用法

    在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...

随机推荐

  1. redis 缓存击穿 看一篇成高手系列3

    什么是缓存击穿 在谈论缓存击穿之前,我们先来回忆下从缓存中加载数据的逻辑,如下图所示 因此,如果黑客每次故意查询一个在缓存内必然不存在的数据,导致每次请求都要去存储层去查询,这样缓存就失去了意义.如果 ...

  2. Cookie 简单设置使用

    cookie的理解可以为,存放在用户浏览器上的一个文件,里面是键值对数据,用于跟服务端通讯.前端可以通过jquery cookie插件设置和读取,后端直接设置 1.获取Cookie: 1 2 3 4 ...

  3. TCP/IP 通信

    TCP/IP 通信又叫socket 通信,是基于TCP/IP协调面向连接的一个数据传输技术.是属于OSI国际标准的传输层,三次握手 提供数据,有序,安全,端到端的传输和接收.它有三个主要协议:传输控制 ...

  4. 浅谈SPI总线

    SPI总线概述     SPI全称是串行外设接口(Serial Peripheral Interface),是由Motorola提出的一种全双工同步串行通信接口,通信波特率可以高达5Mbps,但具体速 ...

  5. RTP协议

    RTP协议格式: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+ ...

  6. 3、谈谈 Java NIO

    在 JDK1.4 之后,为了提高 Java IO 的效率,Java 提供了一套 New IO (NIO),之所以称之为 New,原因在于它相对于之前的 IO 类库是新增的.此外,旧的 IO 类库提供的 ...

  7. 【原创】Windows上应用程序报错常用分析方法总结

    在日常使用Windows的过程中,经常会遇到应用程序不能正常启动.关闭等使用问题.对于Windows来说,解决这些问题的方法比较多,大多时候我们可以通过百度或谷歌搜索来解决.但更多的时候,我们需要找出 ...

  8. mfc添加自定义事件

    1.在对话框的头文件里面添加声明函数: afx_msg void OnStnClickedPicStop(); 2.在对话框的源文件添加 BEGIN_MESSAGE_MAP(CPcEn3dTestDl ...

  9. Java Web组件Servlet、Filter、Listener

    一.Servlet 类javax.servlet.http.HttpServlet; 1.是单例模式,一个web容器中只有一个实例.服务器调用它的service方法进行请求处理, service方法又 ...

  10. 使用Microsoft自带的小工具将可执行文件(.exe)注册为系统服务

    首先,我们从Microsoft下载Windows Resource Kits,Download 下载完成后,运行rktools.exe进行安装. 安装完成后,我们打开安装目录,将其中的"in ...