config:配置    parser:解析

此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser,在 python 2.x 里名字为 ConfigParer

来看一个好多软件的常见配置文件格式如下

```cnf
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
```

解析

```py
>>> import configparser # 导入模块
>>> config = configparser.ConfigParser() #实例化(生成对象)
>>> config.sections() #调用sections方法
[]
>>> config.read('example.ini') # 读配置文件(注意文件路径)
['example.ini']
>>> config.sections() #调用sections方法(默认不会读取default)
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config #判断元素是否在sections列表内
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User'] # 通过字典的形式取值
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
''
>>> for key in config['bitbucket.org']: print(key) # for循环 bitbucket.org 字典的key
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'
```

其他增删改查语法

import configparser

config = configparser.ConfigParser()
config.read('conf_test.ini') ########## 读 ##########
secs = config.sections() #获取结点的值
print(secs)
options = config.options('group2') # 获取指定section的keys
print(options) item_list = config.items('group2') # 以元组的形式获取指定 section 的 keys & values ,key value
print(item_list) val = config.get('group1','key') # 获取指定的key 的value
val = config.getint('group1','key') # 获取指定key 的value ,其中value必须为 int 类型 ########## 改写 ##########
sec = config.remove_section('group1') # 删除section 并返回状态(true, false)
config.write(open('conf_test.ini', "w")) # 每个对应的修改操作都要写入文件才会生效 sec = config.has_section('wupeiqi') #section 中是否存在 wupeiqi
sec = config.add_section('wupeiqi') #添加section
config.write(open('conf_test.ini', "w")) config.set('group2','k1','') #向section中添加key及其对应value,其中key和value都必须是str
config.write(open('conf_test.ini', "w")) config.remove_option('group2','age') #删除指定section中的指定key及其对应value值
config.write(open('conf_test.ini', "w"))

Python全站之路----常用模块----configparser模块的更多相关文章

  1. python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射

    目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...

  2. Python3学习之路~5.11 configparser模块

    用于生成和修改常见配置文档,当前模块的名称在 python 2.x 版本中为 ConfigParser, python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式 ...

  3. python学习第五十三天configParser模块的使用

    configParser 模块用于生成和修改常见配置文档,python 3.x为configParser,配置软件的常见配置格式 模块的用法 import configparser config=co ...

  4. Python第十一章-常用的核心模块01-collections模块

    python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...

  5. Python第十一章-常用的核心模块03-json模块

    python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...

  6. Python第十一章-常用的核心模块04-datetime模块

    python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...

  7. python之shelve、xml、configparser模块

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

  8. python学习之路-第四天-模块

    模块 sys模块 sys.argv:参数列表,'using_sys.py'是sys.argv[0].'we'是sys.argv[1].'are'是sys.argv[2]以及'arguments'是sy ...

  9. 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...

随机推荐

  1. CentOS7+Win10双系统的CentOS7启动bug

    自从安装了CentOS7系统,它的启动时间常常要三分钟以上,实在忍不了. 以下是CentOS7系统的启动记录片断: May 18 13:04:05 DESKTOP-23V3CHU kernel: XF ...

  2. linux常用命令 sort排序命令

    排序命令sort sort [选项] 文件名 选项 -f 忽略大小写 -n 以数值型进行排序,默认使用字符串型进行排序 -r 反向排序 -t 指定分割符,默认的分割符是制表符 -k n[,m] 安装自 ...

  3. js 调用后台,后台调用js

    <html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat=" ...

  4. Android开发笔记---adb命令

    adb命令的使用: adb shell命令:pm list packages -f:显示包名称及相应的APK文件

  5. springboot秒杀课程学习整理1-1

    1)新建一个maven工程quickStart,然后在pom文件里添加依赖 <parent> <groupId>org.springframework.boot</gro ...

  6. 语法、id和class选择器、创建、

    一. 1.CSS规则由两个主要部分构成:选择器,以及一条或多条声明(每条声明由一个属性和一个值构成,属性和值被冒号分开). 2.声明以分号“:”结束,生命组用大括号“{}”括起来. [示例:p {co ...

  7. Oracle 11g 单实例到单实例OGG同步实施文档-RMAN 初始化

    Oracle 11g 单实例到单实例OGG同步实施文档-RMAN 初始化 2018-06-07 13:455170原创GoldenGate 作者: leo 本文链接:https://www.cndba ...

  8. OpenCV(一):集成

    开这个系列文章,记录自己在项目中使用OpenCV所走的步骤流程,方便以后再次使用.如果有不正确的地方,欢迎指正. OpenCV在其官网上,已经发布有iOS版的库,但是只有核心模块的功能,扩展模块(比如 ...

  9. Windows Server 2008 R2 服务器系统安装及配置全过程图文详解

    前言 本文主要介绍了 windows Server 2008 R2 服务器系统的安装及相关配置. 介绍的是以优盘的方式安装. 写这篇博文的目的一来是为了供有需要的网友参考, 二来自己也在此做个记载. ...

  10. mybatis源码解析之Configuration加载(五)

    概述 前面几篇文章主要看了mybatis配置文件configuation.xml中<setting>,<environments>标签的加载,接下来看一下mapper标签的解析 ...