python3 之configparser 模块
configparser 简介
configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近
[db]
db_count = 3
1 = passwd
2 = data
3 = ddf
4 = haello
“[ ]”包含的为 section,section 下面为类似于 key - value 的配置内容;
configparser 默认支持 ‘=’ ‘:’ 两种分隔。
import configparser
import os
def get_db():
config=configparser.ConfigParser() #调用配置操作句柄
config_sec=config.sections() #查看所有内容 现在还是[]
print(config_sec)
Filepath="/home/python/tmp/db.ini"
if os.path.exists(Filepath):
config.read(Filepath) #python3 中要用read
db_count=config.get("db","db_count") #查看db下面db_count的值
db_count=int(db_count) #转化为数值
print(db_count)
print(config.items("db")) #查看所有的值
if __name__=='__main__': #程序入口
get_db() #执行函数
>>> for i in config.items('db'):
... print(i)
...
('db_count', '3')
('1', 'passwd')
('2', 'data')
('3', 'ddf')
('4', 'haello')
>>> config.get('db','1')
'passwd'
检查:
>>> '1' in config['db']
True
>>> 'passwd' in config['db']
False
>>>
添加:
>>> config.add_section('Section_1')
>>> config.set('Section_1', 'key_1', 'value_1') # 注意键值是用set()方法
>>> config.write(open('db.ini', 'w')) # 一定要写入才生效
删除:
>>> config.remove_option('Section_1', 'key_1')
True
>>> config.remove_section('Section_1')
True
>>> config.clear() # 清空除[DEFAULT]之外所有内容
>>> config.write(open('db.ini', 'w'))
import configparser
import os
def get_db():
config=configparser.ConfigParser()
config_sec=config.sections()
print(config_sec)
Filepath="/home/python/tmp/db.ini"
if os.path.exists(Filepath):
config.read(Filepath)
db_count=config.get("db","db_count")
db_count=int(db_count)
print(db_count)
print(config.items("db"))
allrules=[]
for a in range(1,db_count+1):
allrules.append(config.get("db",str(a)))
print( allrules)
else:
sys.exit(6)
if __name__=='__main__':
get_db()
运行结果:
[]
3
[('db_count', '3'), ('1', 'passwd'), ('2', 'data'), ('3', 'ddf'), ('4', 'haello')]
['passwd', 'data', 'ddf']
python3 之configparser 模块的更多相关文章
- Python3之configparser模块
1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...
- (15)-Python3之--configparser模块
1.模块简介 configparser模块是python用来读取配置文件的模块,置文件的格式跟windows下的ini或conf配置文件相似,可以包含一个或多个节(section), 每个节可以有多个 ...
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- Python3 中 configparser 模块用法
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- (转)python的ConfigParser模块
原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...
- 【python3】configparser读取ini配置文件
在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...
- Python3.x:ConfigParser模块的使用
Python3.x:ConfigParser模块的使用 简介 ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节 ...
- Python3 logging模块&ConfigParser模块
''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
随机推荐
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响
笔记 4.SpringCloud微服务核心组件Eureka介绍和闭源后影响 简介: SpringCloud体系介绍 官方地址:http://projec ...
- 如何快速通过json构建javabean对象(Intellij IDEA-->GsonFormat使用教程)
和第三方对接的时候,返回给我们的json时参数字段多是很常见的现象,所以我们手动去创建javabean肯定是要花费不少时间,博主在网上找到了很多种,可用通过json自动生成javabean的工具,这里 ...
- 大数据HIve
1. 题目说明 设计题:MySQL数据库A有1000w条数据,完成统计再输入到另外的B表中 A表 test1 0.2,3.5,1,1test1 1.2,2.3,4.56test2 2.1,0.3,9. ...
- Pearson Correlation Score
[http://www.statisticshowto.com/what-is-the-pearson-correlation-coefficient/] Correlation between se ...
- 刚开始使用idea的朋友,可以看一下下面这篇文章
刚开始使用idea的朋友,可以点击本链接看一下这篇文章 以及这些文章 http://www.jetbrains.com/help/idea/getting-help.html------ Gettin ...
- URLSearchParams的注意事项(个人总结)
官网解释:URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串,一些方法的使用. 前天测试说 有些功能在ie实现不了,顺便把报错发我了:URLSearchParams ...
- 在VM虚拟机Windows Server r2上部署安装Microsoft Dynamics CRM 2016 步骤详解(一)
应公司需求,最近在学微软的Dynamics CRM.在搭建环境的过程中也遇到了一些雷坑,在这里分享一下安装部署过程当中所遇到的一些问题, 安装Microsoft Dynamics CRM 2016的几 ...
- 如何限制nginx的响应速率
参考官方地址:http://nginx.org/en/docs/http/ngx_http_core_module.html#variables 用$limit_rate内置的变量可以限制nginx的 ...
- git stash save -a 遇到的坑 , 弹出匿藏错误
情景一: 用命令行的 : git stash save -u "描述" git stash save -a "描述" -u: 会把没有记录到的文件也保存下来(比 ...
- Clone()方法详解
一.克隆的原理与应用 clone在堆上分配内存,分配的内存和源对象(即调用clone方法的对象)相同,然后再使用原对象中对应的各个域,填充新对象的域, 填充完成之后,clone方法返回,一个新的相同的 ...