python configparser模块_Python标准库之ConfigParser模块
ConfigParser模块用于生成和修改常见配置文档。
比如配置文件格式如下:
[DEFAULT]
ServerAliveInterval= 45Compression=yes
CompressionLevel= 9ForwardX11=yes
[bitbucket.org]
User=hg
[topsecret.server.com]
Port= 50022ForwardX11= no
生成一个配置文件
import configparser
cfp = configparser.ConfigParser()
cfp['DEFAULT'] = {'ServerAliveInterval':'45','Compression':'yes','CompressionLevel':'9','ForwardX11':'yes'}
cfp['bitbucket.org'] = {'User':'hg'}
cfp['topsecret.server.com'] = {'Port':'50022','ForwardX11':'no'}
with open("test.ini",'w') as confile:
cfp.write(confile)
运行后,在当前目录下生成了一个test.ini文件,文件内容如下:
读配置文件
defaults返回的是元组类型。
import configparser
cfp = configparser.ConfigParser()
cfp.read("test.ini")
print(cfp.defaults())
print(cfp.sections())
print(cfp['bitbucket.org']['user'])
运行结果如下:
遍历读取
import configparser
cfp = configparser.ConfigParser()
cfp.read("test.ini")
for i in cfp.defaults():
print(i,cfp.defaults()[i])
运行结果:
增删
删section
cfp.read("test.ini")
sec = cfp.remove_section('bitbucket.org')
cfp.write(open('test.ini', "w"))
删option:
cfp.read("test.ini")
sec = cfp.remove_option('topsecret.server.com','port')
cfp.write(open('test.ini', "w"))
增section:
cfp.read("test.ini")
sec = cfp.add_section('xxxx.server.com')
cfp.write(open('test.ini', "w"))
增option:
cfp.read("test.ini")
sec = cfp.set('topsecret.server.com','port',"5002")
cfp.write(open('test.ini', "w"))
python configparser模块_Python标准库之ConfigParser模块相关推荐
- python itertools模块_Python标准库:itertools模块
来源:j_hao104 简介 官方描述:Functional tools for creating and using iterators.即用于创建高效迭代器的函数. itertools.chain ...
- python标准库对象导入语句_Python标准库之Sys模块使用详解
sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称. 使用sy ...
- python字符串库函数_Python标准库概览(1):string
Python的 string 标准库保留了一些有用的函数和用于处理文本对象的类,现在我们来一起看一下Python的string标准库还有哪些我们不知道的有趣用法? 01.capwords()函数:将字 ...
- python groupby用法_Python 标准库实践之合并字典组成的列表
作者 | 陈祥安 来源 | Python学习开发 最近做了个数据合并的需求这里记录下: 要求如下: dict_of_list1 = [{"name": "zhangsan ...
- python文件管理包_Python标准库04 文件管理 (部分os包,shutil包)
Python标准库04 文件管理 (部分os包,shutil包) 在操作系统下,用户可以通过操作系统的命令来管理文件,参考.Python 标准库则允许我们从 Python 内部管理文件.相同的目的,我 ...
- print python 带回车_python标准库threading源码解读【二】
紧接着上一篇文章继续解析源码 甘蔗:python标准库threading源码解读[一]zhuanlan.zhihu.com 目录 Event的介绍和用法 Event源码解析 以后的内容尽量少一点并且 ...
- python标准库模块教程_Python标准库之Sys模块使用详解
sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称. 使用sy ...
- python中random库_python标准库之random模块
Python中的random模块用于生成随机数. 下面具体介绍random模块的功能: 1.random.random() #用于生成一个0到1的 随机浮点数:0<= n < 1.0 1 ...
- python解析xml文件选用模块_Python标准库系列之xml模块
Python's interfaces for processing XML are grouped in the xml package. 带分隔符的文件仅有两维的数据:行和列.如果你想在程序之间交 ...
最新文章
- 数据库锁解决并发问题
- python编程自学能学会吗-自学Python会有什么困难?老男孩自学python编程
- Mysql 存储引擎和事物基础概念
- 稀疏和L1正规化(Sparsity and Some Basics of L1 Regularization )
- js: live和die,bind和unbind
- 趣味编程:C#中Specification模式的实现
- 前后端交互json字符串
- ubuntu 网卡突然无法连接
- Redis——学习之路一(初识redis)
- ElasticSearch和solr的对比
- flutter listview 滚动到底部_??一个高颜值Flutter版WanAndroid客户端
- 数据科学的原理与技巧 二、数据生成
- 通用商品后端管理系统
- 百度网盘加速下载Motrix多线程下载器
- 一个人的生命周期,就是人的一生
- 在线SVG转换,支持SVG to PNG、SVG to JPEG、SVG to WEBP 图片转换操作-toolfk程序员在线工具网
- 5G牌照发放了,但需要购买5G手机的用户应等明年再购买
- 桌面窗口管理器占用内存过高 - 解决方案
- 微信JSAPI支付教程
- 李群SE(3)即欧式变换Euclidean transformation(刚性变换Rigid Transformation)