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 模块的更多相关文章

  1. Python3之configparser模块

    1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...

  2. (15)-Python3之--configparser模块

    1.模块简介 configparser模块是python用来读取配置文件的模块,置文件的格式跟windows下的ini或conf配置文件相似,可以包含一个或多个节(section), 每个节可以有多个 ...

  3. Python3 中 configparser 模块解析配置的用法详解

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  4. Python3 中 configparser 模块用法

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  5. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  6. 【python3】configparser读取ini配置文件

    在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...

  7. Python3.x:ConfigParser模块的使用

    Python3.x:ConfigParser模块的使用 简介 ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节 ...

  8. Python3 logging模块&ConfigParser模块

    ''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...

  9. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

随机推荐

  1. 查看openstack各组件小版本号

    查看api版本号 因为要判断openstack的某个小功能是否支持,需要确定api版本号大于某个版本,故需要查询现有环境api版本号 如查看cinder-api的版本号 指定api版本调api 在he ...

  2. RabbitMQ学习之:(三)第一个RMQ的程序 (转贴+我的评论)

    RabbitMQ for Windows: Building Your First Application Posted by Derek Greer on March 7, 2012 This is ...

  3. [spring mvc][转]<mvc:default-servlet-handler/>的作用

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  4. RedHat Enterprise7 搭建ISCSI

    IP 主机名 作用 10.45.129.113/24      外网 172.16.1.10/24        内网 rac1 RAC节点1 10.45.129.114/24      外网 172 ...

  5. JAVA 基础编程练习题12 【程序 12 计算奖金】

    12 [程序 12 计算奖金] 题目:企业发放的奖金根据利润提成.利润(I)低于或等于 10 万元时,奖金可提 10%:利润高于 10 万元, 低于 20 万元时,低于 10 万元的部分按 10%提成 ...

  6. 关键字 using语句 大神的神扯

    using 是非托管资源: 解析:在C#应用托管到.NET Framework.但是他可以释放非托管资源. using 关键字有两个作用: 1:作为关键字,using可以导入命名空间 2:座位C#语句 ...

  7. window 安装指定的node版本

    有时候不同的项目需要不同的node版本,window切换node版本命令很不管用,甚至需要卸载后重新装,同事分享了一下他的做法,很便利. 1.打开node官网 https://nodejs.org/e ...

  8. Mac 安装Mysql 之 Sqlservice 区别

    Mysql 一.下载mysql    官网“Community “ 下会看到“MySQL Community Server”下方有一个“download”点击.   在Mac OS上的MySQL的版本 ...

  9. (转)window.XMLHttpRequest详解(AJAX工作原理)

    转自:http://l.xbest.blog.163.com/blog/static/8640444120100225516963/?fromdm&fromSearch&isFromS ...

  10. HA cluster

    ------------恢复内容开始------------ 集群分类: LB负载均衡集群(lvs/nginx(http/upstream, stream/upstream)),HA高可用集群,HP高 ...