configparser模块用来对配置文件进行操作

1、获取所有块

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('xxxooo', encoding='utf-8')
  5. ret = config.sections()
  6. print(ret)

2、获取指定块下所有的键值对

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('xxxooo', encoding='utf-8')
  5. ret = config.items('section1')
  6. print(ret)

3、获取指定块下所有的建

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('xxxooo', encoding='utf-8')
  5. ret = config.options('section1')
  6. print(ret)

4、获取指定块下指定key的值

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('xxxooo', encoding='utf-8')
  5.  
  6. v = config.get('section1', 'k1')
  7. # v = config.getint('section1', 'k1')
  8. # v = config.getfloat('section1', 'k1')
  9. # v = config.getboolean('section1', 'k1')
  10.  
  11. print(v)

5、检查、删除、添加块

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('xxxooo', encoding='utf-8')
  5.  
  6. # 检查
  7. has_sec = config.has_section('section1')
  8. print(has_sec)
  9.  
  10. # 添加块
  11. config.add_section("SEC_1")
  12. config.write(open('xxxooo', 'w'))
  13.  
  14. # 删除块
  15. config.remove_section("SEC_1")
  16. config.write(open('xxxooo', 'w'))

6、检查、删除、设置指定组内的键值对

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('xxxooo', encoding='utf-8')
  5.  
  6. # 检查
  7. has_opt = config.has_option('section1', 'k1')
  8. print(has_opt)
  9.  
  10. # 删除
  11. config.remove_option('section1', 'k1')
  12. config.write(open('xxxooo', 'w'))
  13.  
  14. # 设置
  15. config.set('section1', 'k10', "")
  16. config.write(open('xxxooo', 'w'))

Python模块 - configparser的更多相关文章

  1. 保留注释换行的python模块configparser

    python语言用来解析配置文件的模块是ConfigParser,python3中是configparser模块,我在使用中发现write方法在将配置项重新写入文 件时,配置文件中的空行和注释行都会被 ...

  2. Python模块configparser(操作配置文件ini)

    configparser模块提供对ini文件的增删改查方法. ini文件的数据格式: [name1] attribute1=value1 attribute2=value2 [name2] attri ...

  3. Python模块——configparser

    configparser模块 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值) 创建文件 import configp ...

  4. python 模块之-configparser

    python 模块configparser   配置文件模块 import configparser    config = configparser.ConfigParser() config[&q ...

  5. python模块(shelve,xml,configparser,hashlib,logging)

    1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...

  6. python模块基础之json,requeste,xml,configparser,logging,subprocess,shutil。

    1.json模块 json     用于[字符串]和 [python基本数据类型] 间进行转换(可用于不同语言之前转换),json.loads,将字符串转成python的基本数据类型,json.dum ...

  7. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  8. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  9. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

随机推荐

  1. Pla

    Pla(jdoj1006) 题目大意:给你n个矩形,并排放在一起,你的目的是将所有的矩形全部染色.你每次染的形状为一个矩形,问:最少需要染多少次? 注释:n<=10^6,wi , hi<= ...

  2. hibernate框架学习笔记1:搭建与测试

    hibernate框架属于dao层,类似dbutils的作用,是一款ORM(对象关系映射)操作 使用hibernate框架好处是:操作数据库不需要写SQL语句,使用面向对象的方式完成 这里使用ecli ...

  3. JavaScript(第三十三天)【总结:封装基础前端框架】

    源码地址:https://github.com/whisper540/Base

  4. 第六次作业:Alpha阶段综合报告(Java-Team)

    团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://github.com/WHUSE201 ...

  5. Beta冲刺置顶随笔

    项目名称:城市安全风险管控系统 小组成员: 张梨贤.林静.周静平.黄腾飞 Beta冲刺随笔 Beta预备 Beta冲刺Day1 Beta冲刺Day2 Beta冲刺Day3 Beta冲刺Day4 Bet ...

  6. numpy.random.seed()方法

    先贴参考链接: https://stackoverflow.com/questions/21494489/what-does-numpy-random-seed0-do numpy.random.se ...

  7. 项目Beta冲刺Day7

    项目进展 李明皇 今天解决的进度 部分数据传递和使用逻辑测试 林翔 今天解决的进度 服务器端查看个人发布的action,修改已发布消息状态的action,仍在尝试使用第三方云存储功能保存图片 孙敏铭 ...

  8. System.Reflection名称空间下的程序集类Assembly应用.

    利用反射中的程序集类(Assembly--抽象类)动态加载类库(.dll)或者可执行程序(.exe). 优点:①.可以消除if条件的逻辑判断.②.减少内存资源.③.有利于程序扩展. 缺点... 使用静 ...

  9. Django-rest-framework源码分析----权限

    添加权限 (1)API/utils文件夹下新建premission.py文件,代码如下: message是当没有权限时,提示的信息 # utils/permission.py class SVIPPr ...

  10. Python内置函数(49)——isinstance

    英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...