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. WPF下如何去除WebBrowser的滚动条和捕获关闭事件

    方法一:适用于VS2008 1.在解决方案中添加“引用”     选择 COM 下的 Microsoft html object library 2.引入命名空间     using mshtml; ...

  2. Laplacian eigenmap 拉普拉斯特征映射

    下面是实验室大牛师兄自己写的一段总结,主要内容是Laplacian Eigenmap中的核心推导过程. 有空还是多点向这位师兄请教,每次都会捡到不少金子. Reference : <Laplac ...

  3. JAVA 基础编程练习题5 【程序 5 判断分数等级】

    5 [程序 5 判断分数等级] 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90 分的同学用 A 表示,60-89 分之间的用 B 表示, 60 分以下的用 C 表示. 程序分析:(a&g ...

  4. JAVA 基础编程练习题2 【程序 2 输出素数】

    2 [程序 2 输出素数] 题目:判断 101-200 之间有多少个素数,并输出所有素数. 程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除,则表明此数不是素数, ...

  5. 手写web框架之开发一个类加载器

    ackage io.renren.common; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUti ...

  6. React Native安卓代码混淆和打包

    一上午就整了个React Native的打包,中间还遇到各种问题,这里还是记录下吧: 文档链接: http://reactnative.cn/docs/0.45/signed-apk-android. ...

  7. launchImage设置后在启动时无法显示

    有人问我他的APP设置了启动页,然后居然不显示.....我觉得应该不可能啊,然后我自己再次实现了一下设置启动页,这个问题好像以前从来没有注意过,也没有很深刻的掌握APP启动页的设置和注意事项,今天遇到 ...

  8. dtcms 手机浏览

    private string GetSitePath(string webPath, string requestPath, string requestDomain) { //获取当前域名包含的站点 ...

  9. 用Python操作Excel,实现班级成绩的统计

    本次是在原来有一定格式的Excel文档中补充成绩. 安装的模块:xlwt . xlrd .xlutils xlrd的模块是只用读取xls文件,不能写文件,同理xlwt,只(新建写)不读已有的xls, ...

  10. linux无密钥登陆

    1.在用户目录下执行命令 ssh-keygen -t rsa 一路回车: 2.在当前用户目录下,进入.ssh文件夹(.ssh文件夹为隐藏文件夹,直接进去即可). 在.ssh文件夹下执行命令: cat ...