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. Nginx 负载均衡的Cache缓存批量清理的操作记录

    1)nginx.conf配置 [root@inner-lb01 ~]# cat /data/nginx/conf/nginx.conf user www; worker_processes 8; #e ...

  2. Python初始编码-3

    01010100 新11010000 开11010100 一01100000 家11000000 看11000000 看 01010100011101110101011110110A B C01000 ...

  3. 领跑衫获奖感言 & 课程总结

    很荣幸在最后一次课获得了黄色领跑衫.在此,我要感谢教师杨贵福,感谢<构建之法>的作者邹欣老师和出版人周筠老师,感谢“耐撕”团队的队员们. 作为旁听生,最后一堂课,有些不舍.不多说,先上图, ...

  4. 五子棋游戏SRS文档

        SRS技术文档,包括对SRS的解释说明.SRS描述规范.软件需求规格说明书(SRS,Software Requirement Specification)是为了软件开发系统而编写的,主要用来描 ...

  5. Leetcode——171.Excel表列序号【水题】

    @author: ZZQ @software: PyCharm @file: leetcode171_Excel表列序号.py @time: 2018/11/22 15:29 要求: 给定一个Exce ...

  6. Using svn in CLI with Batch

    del %~n0.txt@echo offsetlocal EnableDelayedExpansionfor /f "delims=" %%i in ('DIR /A:D /B' ...

  7. 【转帖】ARM的两种不同的CPU docker 应该也是支持arm的

    armel和armhf区别选择 知识经验  3年前 (2014-11-07)  20603浏览  1评论 目录 fpu单元 armel与armhf 安装armel和armhf arm-linux-gn ...

  8. Jquery 事件冒泡、元素的默认行为的阻止、获取事件类型、触发事件

    $(function(){// 事件冒泡 $('').bind("click",function(event){ //事件内容 //停止事件冒泡 event.stopPropaga ...

  9. html web服务器

    web服务器可以发布网站,从而使得网站可以被全世界访问: 在自己服务器上托管自己的网站的要求: 硬件搭建:强大的服务器硬件,保证24小时高速连接, 软件搭建:服务器授权比客户端授权更贵: 人工费:环境 ...

  10. Java之数组遍历

    package basic; //数组遍历方法 public class ForEach { public static void main(String[] args) { // 原始数组 Stri ...