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. django中iframe问题

    因为在django中无法识别我们普通的url格式,比如使用<iframe src="articles.html"></iframe>,这种格式django无 ...

  2. mariadb数据库(1)

    一.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理数据库里的数 ...

  3. java+目录上传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  4. Servlet(2):Requset/Response Encoding and Filter

    Requset/Response Encoding 表单提交分GET和POST,接下来分开讨论. (1)GET/URL提交的数据 在 Tomcat中,默认情况下使用"URIEncoding& ...

  5. VueRouter爬坑第三篇-嵌套路由

    VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...

  6. 消息中间件RabbitMq的代码使用案例

    消费者: ---------------------- 构造初始化: public RabbitMqReceiver(String host, int port, String username, S ...

  7. 小记--------spark的两种提交模式

    spark的两种提交模式:yarn-cluster . yarn-client 图解

  8. C++中用ODBC和ADO方式连接SQL数据库

    https://wenku.baidu.com/view/f01e4e762f3f5727a5e9856a561252d380eb2033.html

  9. Treasure Island(两遍dfs)-- Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises)

    题意:https://codeforc.es/contest/1214/problem/D 给你一个n*m的图,每次可以往右或者往下走,问你使(1,1)不能到(n,m)最少要放多少 ‘ # ’ . 思 ...

  10. Vue.js学习笔记-script标签在head和body的区别

    初学JavaScript,项目需要没有系统学习,只能边查资料边码代码,埋下的坑不知道有多少,还是建议时间充足的情况下系统的将Javascript学习一遍 ,涉及的HTML知识也务必了解. 问题 最开始 ...