configParse模块(二十七)
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模块(二十七)的更多相关文章
- 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 ...
- 【转】Python3 configparse模块(配置)
[转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...
- python模块: hashlib模块, configparse模块, logging模块,collections模块
一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...
- configParse模块
一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...
- 常用模块二(hashlib、configparser、logging)
阅读目录 常用模块二 hashlib模块 configparse模块 logging模块 常用模块二 返回顶部 hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SH ...
- NGINX模块(二)
[Nginx标准HTTP模块] 一.HTTP核心模块 指令1:alias 语法:alias file-path|directory-path; 默认值:no 使用字段:location 说明:这个指令 ...
- Bootstrap<基础二十七> 多媒体对象(Media Object)
Bootstrap 中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如:博客评论),我们可以在组件中使用图文混排,图像可以左对齐或者右对齐.媒体对象可以用更少的 ...
- Web 开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十七】
<Web 前端开发精华文章推荐>2014年第6期(总第27期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- Citrix 服务器虚拟化之二十七 XenApp6.5发布服务器桌面
Citrix 服务器虚拟化之二十七 XenApp6.5发布服务器桌面 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1) 服务器桌面:发布场中服务器的整个 ...
- 转:二十七、Java图形化界面设计——容器(JFrame)
转:http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计——容器(JFrame) 程序是为了方便用户使用的, ...
随机推荐
- Linux下绑定网卡的操作记录
公司采购的服务器安装了双网卡,并进行bond网卡绑定设置,网卡绑定mode共有七种(0~6) bond0.bond1.bond2.bond3.bond4.bond5.bond6. 第一种模式:mod= ...
- MongoDB副本集(一主一备+仲裁)环境部署-运维操作记录
MongoDB复制集是一个带有故障转移的主从集群.是从现有的主从模式演变而来,增加了自动故障转移和节点成员自动恢复.MongoDB复制集模式中没有固定的主结点,在启动后,多个服务节点间将自动选举产生一 ...
- LInux系统木马植入排查分析 及 应用漏洞修复配置(隐藏bannner版本等)
在日常繁琐的运维工作中,对linux服务器进行安全检查是一个非常重要的环节.今天,分享一下如何检查linux系统是否遭受了入侵? 一.是否入侵检查 1)检查系统日志 检查系统错误登陆日志,统计IP重试 ...
- 第八周--Linux中进程调度与进程切换的过程
[潘恒 原创作品转载请注明出处 <Linux内核分析>MOOC课程 "http://mooc.study.163.com/course/USTC 1000029000 " ...
- Linux内核分析作业六
1.阅读理解task_struct数据结构 2.分析fork函数对应的内核处理过程sys_clone,理解创建一个新进程如何创建和修改task_struct数据结构: fork进程的代码 #inclu ...
- JAVA链表中迭代器的实现
注:本文代码出自<java数据结构和算法>一书. PS:本文中类的名字定义存在问题,Link9应改为Link.LinkList9应该为LinkList.由于在同包下存在该名称,所以在后面接 ...
- 1.个人项目 Individual Project
https://github.com/sunlitao 一. 实验1通讯录管理系统 通讯录中的联系人包含以下信息项:姓名.手机.办公电话.家庭电话.电子邮箱.所在省市.工作单位.家庭住址,群组分类(亲 ...
- spring 整合
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- PAT 1026 程序运行时间
https://pintia.cn/problem-sets/994805260223102976/problems/994805295203598336 要获得一个C语言程序的运行时间,常用的方法是 ...
- Atlas & mysql-proxy
Atlas https://github.com/Qihoo360/Atlas https://github.com/Qihoo360/Atlas/wiki/Installing-Atlas Atla ...