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. css-preprocessors

    what ? 预处理器是css 能够使用 变量.操作符.函数.mixins.interpolations 等类似于js 功能的一种语言. 目前比较常用是三种:SASS.less .stylus . W ...

  2. 安卓开发helloworld

    https://blog.csdn.net/tangjie134/article/details/79495204

  3. [2019BUAA软件工程]结对编程感想

    结对编程感想 写在前面   本博客为笔者在完成软件工程结对编程任务后对于编程过程.最终得分的一些感想与经验分享.此外笔者还对于本课程的结对编程部分提出了一些建议. Tips Link 作业要求博客 2 ...

  4. 第三个Sprint ------第二天

    主界面代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. shell脚本--文件查找之find命令

    首先是通过文件名称来查找,需要使用一个-name参数. 查询以  .txt结尾的文件,和以 t 开头的文件: ubuntu@ubuntu:~/test$ ls one.txt three.txt tw ...

  6. Java的常用命令javac与java

    javac 可以使用javac -h来查看常用的命令: -> ~ # javac -help 用法: javac <options> <source files> 其中, ...

  7. PAT 1057 数零壹

    https://pintia.cn/problem-sets/994805260223102976/problems/994805270914383872 给定一串长度不超过 10​5​​ 的字符串, ...

  8. 临时关闭Mysql ONLY_FULL_GROUP_BY

    /** * @author lcc807@ikoo8.com * * 临时关闭Mysql ONLY_FULL_GROUP_BY */ function closeSqlFullMode(){ DB:: ...

  9. ubuntu 环境 celery配置全解

    继续尝试没有时间弄明白的技术. celery官方文档地址:http://docs.celeryproject.org/en/stable/getting-started/introduction.ht ...

  10. 关于MySQL中添加数据的两种方法

    下面介绍两种执行SQL命令的方法,并作出相应地总结,第一种介绍一种常规用法,下面进行做简要地分析,首先我们需要执行打开数据库操作首先创建一个MySqlConnection对象,在其构造函数中传入一个连 ...