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. Kettle解决方案: 第二章 Kettle基本概念

    2概述 设计模块最主要的操作分为: 转换和作业 选择转换和作业后就可以选择对应主对象树和核心对象 主对象树大同小异 核心对象是不同的 比如转换需要用到的CSV表输入, 表输入等都在这里可以选择 而作业 ...

  2. SpringBoot集成Atomikos使用Oracle数据库mybatis、jta框架

    项目中需要数据库分布式事物的实现,于是采用了atumikos技术. 因为生产上需要稳定,所以采用了springboot 1.5.9.RELEASE版本. 本文代码gitlab下载地址: https:/ ...

  3. jenkins 构建完毕后接着构建另外一个构建的方法

  4. Java核心-多线程-并发控制器-CountDownLatch倒数闩

    1.基本概念 CountDownLatch,中文名倒数闩,jdk并发工具包中一个并发控制器,它抽象了一个常见的多线程并发场景,开发人员使用它可以写出同时兼顾线程安全性与高效率的代码. 2.抽象模型 相 ...

  5. idea properties文件中文无法正常显示

    引用:https://blog.csdn.net/u010999809/article/details/81204457 问题:在idea打开配置文件,已经设置了全局编码格式为UTF-8,和项目编码格 ...

  6. Spring AOP使用注解记录用户操作日志

    最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...

  7. CBSN NEWS

    https://www.cbsnews.com/video/fatal-crossing/

  8. Docker使用Link与newwork在容器之间建立连接

    一,使用 --link容器互联 docker 默认使允许container 互通的(通过-icc=false 关闭互通)同一个宿主机上的多个docker容器之间如果想进行通信,可以通过使用容器的ip地 ...

  9. centos上 小程序部署 nginx+https+ssL 提示错误:对应的服务器 TLS 为 TLS 1.0 ,小程序要求的 TLS 版本必须大于等于 1.2

    解决办法: 1.nginx版本必须是1.0.6以上 2.修改配置文件,修改server内的ssl_protocols内容,增加TLSv1.2 3.重启nginx. ./nginx -s reload ...

  10. 欢迎来到GitHub世界

    什么是GitHub GitHub(Pronunciation:/githʌb/)  这是一个为开发者提供Git仓库的托管服务,这是一个让开发者们共享代码的完美场所.GitHub公司总部位于美国旧金山, ...