configparser用于处理特定格式的文件,其本质上是利用open来操作文件。

# 注释1
; 注释2 [section1] # 节点
k1 = v1 # 值
k2:v2 # 值 [section2] # 节点
k1 = v1 # 值 指定格式

生成.ini

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval':'',
'Compression':'yes',
'CompressionLevel':''
}
config['bitbucket.org'] = { }
config['bitbucket.org']['User'] = 'abc'
config['topsecret.server.com'] = { }
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = ''
topsecret['ForwardX11'] = 'no'
config["DEFAULT"]['ForwardX11'] = 'yes' with open('example.ini','w') as configfile:
config.write(configfile)
[DEFAULT]
compression = yes
serveraliveinterval = 45
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = abc [topsecret.server.com]
host port = 50022
forwardx11 = no

读取

import configparser

config = configparser.ConfigParser()
config.read('example.ini') # 查看所有标题
res = config.sections()
print(res) # ['bitbucket.org', 'topsecret.server.com'] # 查看标题section下所有的key=value的key ,DEFAULT 的key会在每一个子项中出现
options = config.options('bitbucket.org')
print(options) # ['user', 'passwd', 'compression', 'serveraliveinterval', 'compressionlevel', 'forwardx11'] # 查看标题section1下所有key=value的(key,value)格式
item_list=config.items('bitbucket.org')
print(item_list)
# [('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'abc'), ('passwd', '123')] # 查看标题section1下user的值=>字符串格式
val = config.get('bitbucket.org','user')
print(val) # abc # 查看标题section1下passwd的值=>整数格式
val1 = config.getint('bitbucket.org','passwd')
print(val1) # # 查看标题section1下is_admin的值=>布尔值格式
val2=config.getboolean('bitbucket.org','is_admin')
print(val2) # True # 查看标题section1下salary的值=>浮点型格式
val3=config.getfloat('bitbucket.org','salary')
print(val3) # 31.0
import configparser

config = configparser.ConfigParser()
config.read('example.ini') # 查看所有标题
res = config.sections()
print(res) # ['bitbucket.org', 'topsecret.server.com'] # 查看标题section下所有的key=value的key ,DEFAULT 的key会在每一个子项中出现
options = config.options('bitbucket.org')
print(options) # ['user', 'passwd', 'compression', 'serveraliveinterval', 'compressionlevel', 'forwardx11'] # 查看标题section1下所有key=value的(key,value)格式
item_list=config.items('bitbucket.org')
print(item_list)
# [('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'abc'), ('passwd', '123')] # 查看标题section1下user的值=>字符串格式
val = config.get('bitbucket.org','user')
print(val) # abc # 查看标题section1下passwd的值=>整数格式
val1 = config.getint('bitbucket.org','passwd')
print(val1) # # 查看标题section1下is_admin的值=>布尔值格式
val2=config.getboolean('bitbucket.org','is_admin')
print(val2) # True # 查看标题section1下salary的值=>浮点型格式
val3=config.getfloat('bitbucket.org','salary')
print(val3) # 31.0

改写


import configparser

config = configparser.ConfigParser()
config.read('example.ini',encoding='utf-8') # 删除整个标题section2
config.remove_section('section2') # 删除标题section1下的某个key
config.remove_option('section1','salary')
config['section1']['is_admin'] = 'False'
config.set('section1','passwd','') # 判断是否存在某个标题
print(config.has_section('section1')) # True # 判断标题section1下是否有user
print(config.has_option('section1','user')) # True # 添加一个标题
config.add_section('egon') # 在标题egon下添加name=egon,age=18的配置
config.set('egon','name','egon')
#config.set('egon','age',18) #报错,必须是字符串
config.set('egon','age','') #最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))

configParse模块(二十七)的更多相关文章

  1. python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为

    python接口自动化测试二十七:密码MD5加密   ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...

  2. 【转】Python3 configparse模块(配置)

    [转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...

  3. python模块: hashlib模块, configparse模块, logging模块,collections模块

    一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...

  4. configParse模块

    一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...

  5. 常用模块二(hashlib、configparser、logging)

    阅读目录 常用模块二 hashlib模块 configparse模块 logging模块   常用模块二 返回顶部 hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SH ...

  6. NGINX模块(二)

    [Nginx标准HTTP模块] 一.HTTP核心模块 指令1:alias 语法:alias file-path|directory-path; 默认值:no 使用字段:location 说明:这个指令 ...

  7. Bootstrap<基础二十七> 多媒体对象(Media Object)

    Bootstrap 中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如:博客评论),我们可以在组件中使用图文混排,图像可以左对齐或者右对齐.媒体对象可以用更少的 ...

  8. Web 开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十七】

    <Web 前端开发精华文章推荐>2014年第6期(总第27期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  9. Citrix 服务器虚拟化之二十七 XenApp6.5发布服务器桌面

    Citrix 服务器虚拟化之二十七  XenApp6.5发布服务器桌面 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务器的整个 ...

  10. 转:二十七、Java图形化界面设计——容器(JFrame)

    转:http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计——容器(JFrame) 程序是为了方便用户使用的, ...

随机推荐

  1. ARM-GPIO

    操作GPIO有三种方法: 调用库函数读取IO的输入电平:uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_t GPIO_pin): 操作寄 ...

  2. Redis常用操作-------Set(集合)

    1.SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略. 假如 key 不存在,则创建一个 ...

  3. 20135218 实践四 ELF文件格式分析

    一 :概述 ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文件用于存储Linux程序.ELF文件(目标文件)格式主要三种: (1)可重定向文件:文 ...

  4. [转帖]TMD为你揭秘中国互联网下半场所有秘密

    https://www.iyiou.com/p/35099.html 李安说,<比利.林恩的中场战事>是“一个成长的故事”.中国互联网也行至中场,下半场如何走,成长的方向在哪里,成当下关键 ...

  5. [转帖] 读懂YML文件.. 书买了还没看完...

    Copy From https://www.cnblogs.com/CloudMan6/p/8370501.html   读懂 Deployment YAML - 每天5分钟玩转 Docker 容器技 ...

  6. 测试 多线程 实现 callable 带返回值

    package threadTest; import java.util.ArrayList; import java.util.Date; import java.util.concurrent.C ...

  7. pandas取值

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang chao # @Fi ...

  8. Null Object Design Pattern (Python recipe)

    Null Object 个人感觉非常有用.也是在review公司其他同事写代码的时候看到. 当时使用了flask的request全局请求变量g,然后使用了g.x保存了一个东西. 当时在view代码读取 ...

  9. Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析

    Hashtable 是一个很常见的数据结构类型,前段时间阿里的面试官说只要搞懂了HashTable,hashMap,HashSet,treeMap,treeSet这几个数据结构,阿里的数据结构面试没问 ...

  10. ssm 整合 redis(进阶教程)

    最后我建议大家使用 Spring StringRedisTemplate 配置,参阅: http://blog.csdn.net/hanjun0612/article/details/78131333 ...