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. [ZZ] NumPy 处理数据

    NumPy-快速处理数据--ndarray对象--数组的创建和存取 https://www.cnblogs.com/moon1992/p/4946114.html NumPy-快速处理数据--ndar ...

  2. 数据分组分析—-groupby

    数据分组分析—-groupby 代码功能: 对于综合表格data,基于title进行分组处理,并统计每一组的size,得到的是一个series序列,此序列可以放入索引中使用,index() impor ...

  3. sql语句可以截取指定字段后面的字符串

    select id,substring(Memo,charindex('数量',Memo)+3,len(Memo)-charindex('数量',Memo)) from trace where Mem ...

  4. UITableView自定义Cell中,纯代码编程动态获取高度

    在UITableView获取高度的代理方法中,经常需要根据实际的模型重新计算每个Cell的高度.直接的做法是在该代理方法中,直接根据模型来返回行高:另 [1]-(CGFloat)tableView:( ...

  5. C#使用NPOI读写Excel的注意事项

    NPOI的基本使用参照:https://www.cnblogs.com/lixiaobin/p/NPOI.html 既存文档读取修改方法 *既存Excel文档修改保存注意使用FileMode.Crea ...

  6. golang实现的简单优先队列

    下面是golang实现的简单优先队列,参考信息可以查看https://golang.org/pkg/container/heap/或者https://golang.google.cn/pkg/cont ...

  7. Android Studio Gradle配置工具开发

    by 蔡建良 2019-3-9 QQ: 304125648 Android Studio导入项目经常出现卡死的情况.针对Gradle更新配置的问题,网上已经有详细的方法,但也很烦索,步骤也很多. 因此 ...

  8. Linux常用的命令以及配置

    cat /etc/group nobody:x:500:用户组 : 口令 : 用户组编号 #查看用户信息 stunnel4:x:118:123::/var/run/stunnel4:/usr/sbin ...

  9. 检查邮箱IP是否在国际反垃圾邮件组织的黑名单中

    有时候邮件发不出去,很有可能就是邮件服务器的IP被国际上一些反垃圾组织列入黑名单了,这时你可以通过返回的邮件判断是否进入黑名单,或者通过以下查询地址看是否被列入,然后一个个申请移除: http://m ...

  10. CUDA编程模型——组织并行线程2 (1D grid 1D block)

    在”组织并行编程1“中,通过组织并行线程为”2D grid 2D block“对矩阵求和,在本文中通过组织为 1D grid 1D block进行矩阵求和.一维网格和一维线程块的结构如下图: 其中,n ...