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. docker-solr 使用host模式修改默认的8983端口号

    1.使用root账号进入docker-solr docker exec -it 127627ab6247 -u root /bin/bash 2.安装vim apt-get update apt-ge ...

  2. __unsafe_unretained的含义

    OC的变量限定词的官方解释: __strong is the default. An object remains “alive” as long as there is a strong point ...

  3. AutoCAD神器! AutoCAD自动切换中英文输入法插件(ZDSRF)

    AutoCAD神器! AutoCAD自动切换中英文输入法插件 (一)功能特点: CAD命令中只能输入英文字符,不能输入中文,在文字编辑.文字输入.尺寸编辑中经常需要输入中文,此时就需要频繁的切换输入法 ...

  4. RAMOS和SSD对比

    http://bbs.pcbeta.com/forum.php?mod=redirect&goto=findpost&ptid=1786284&pid=48341400RAMO ...

  5. 小程序中添加客服按钮contact-button

    小程序的客服系统,是微信做的非常成功的一个功能,开发者可以很方便的通过一行代码,就可实现客服功能. 1. 普通客服按钮添加 <button open-type='contact' session ...

  6. Linux基础上

    文件操作                                                                                                 ...

  7. 怎么安装Scrapy框架以及安装时出现的一系列错误(win7 64位 python3 pycharm)

    因为要学习爬虫,就打算安装Scrapy框架,以下是我安装该模块的步骤,适合于刚入门的小白: 一.打开pycharm,依次点击File---->setting---->Project---- ...

  8. python之路——18

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 有需要<正则指引>这本书的可以联系我,仅供学习交流使用 复习 1.递归函数,必须有结束条件2.返 ...

  9. NPOI 导出Excel部分

    ) { MessageBox.Show("没有找到相关的数据!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.I ...

  10. html/css/js----js中遇到的一些问题

    学习前端的时候有时也会遇到一些弄不明白的问题,学习js会有更多的方法不清楚它的用法,我谨以在学习中遇到的一些问题记录下来,以便日复习! 一."window.opener.location.r ...