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数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
随机推荐
- query和exec区别
1.PDO::query PDO::query执行一条SQL语句,如果通过,则返回一个PDOStatement对象.PDO::query函数有个“非常好处”,就是可以直接遍历这个返回的记录集. 示例如 ...
- List自定义对象的排序,根据对象的某一列进行排序
在工作中,经常需要对List对象集合进行排序操作,下面总结下搞个通用排序对象,原理是使用JAVA的 Comparator 接口实现排序 不多说直接上“干货” 1.存在实体类: @Data @ ...
- Ubuntu 安装 docker,并上传到dockerhub
一.安装Docker apt-get -y install docker.io 链接: ln -sf /usr/bin/docker.io /usr/local/bin/docker 检查docker ...
- JAVA解析_操纵_JS_JAVA_JS引擎
http://uuhorse.iteye.com/blog/1705684 http://blog.csdn.net/chinajash/article/details/1491570 http:// ...
- Fidessa
Fidessa这样为券商提供交易系统和与交易所连接的公司被称作Independent Software Vendor, 同类的还有FIS(前SunGuard), Bloomberg(AIM), Tho ...
- JAVA_day1_变量和常量
JAVA变量和常量 一.JAVA中的关键字 注:关键字区分大小写,Try不是关键字而try则是. 二.JAVA标识符 1.标识符:标识符就是用于给 Java 程序中变量.类.方法等命名的符号. 2.标 ...
- htop/dstat/top/ps命令的使用
top命令 显示系统中进程信息 [root@node0 ~]# top top - 09:36:45 up 13:39, 3 users, load average: 0.02, 0.03, 0. ...
- java使用validator进行校验
不管是html页面表单提交的对象数据还是和第三方公司进行接口对接,都需要对接收到的数据进行校验(非空.长度.格式等等).如果使用if一个个进行校验(字段非常多),这是让人崩溃的过程.幸好jdk或hib ...
- 添加SSH keys到github帐号
使用git clone命令从github上同步github上的代码库时,如果使用SSH链接(如我自己的esesgrid项目:git@github.com:hmilycc/esesgrid.git),而 ...
- Spring Boot常用的注解以及含义<持续更新>
1.@RestController和@RequestMapping注解 @RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boo ...