configparser配置文件处理
创建一个configparser格式的文档: import configparser config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
---------------------------------------------------------
读取configparser的内容:
import configparser
config = configparser.ConfigParser()
print(config.sections()) #没有read之前是空的列表
config.read('example.ini')
print(config.sections()) #打印出非DEFAULT的项
print('bitbucket.org' in config)
print('bytebong.com' in config)
print(config['bitbucket.org']['User'])
print(config['DEFAULT']['Compression'])
topsecret = config['topsecret.server.com']
print(topsecret['FOrwardX11']) #部分中的键不区分大小写并以小写形式存储
print(topsecret['host port'])
for key in config['bitbucket.org']: #注意:把DEFAULT的KEY也打印出来了
print(key)
print(config['bitbucket.org']['ForwardX11'])
---------------------------------------------------------
修改、增加configparser的某些项:
#使用ConfigParser方法修改,增加
import configparser
config = configparser.ConfigParser()
config.read("example.ini") #读到内存中
config.set('DEFAULT','serveraliveinterval','99999') #修改值为99999;value需要是str
config.add_section('Section1') #增加一个Section1项
config.set('Section1', 'an_int', '15') #value需要是str
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
config.write(open('example.ini', "w"))
#使用RawConfigParser方法修改,增加
import configparser
config = configparser.RawConfigParser()
config.read("example.ini") #读到内存中
config.set('DEFAULT','serveraliveinterval','8888') #修改值为8888
config.add_section('Section1') #增加一个Section1项
config.set('Section1', 'an_int', 15) #value可以不是字符串
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
#将配置文件写入到example.ini中
with open('example.ini', 'w') as configfile:
config.write(configfile)
---------------------------------------------------------
删除configparser的某些项:
import configparser
config = configparser.ConfigParser()
config.read("example.ini") #读到内存中
sec = config.remove_section('bitbucket.org') #删除bitbucket.org
config.write(open('example.ini', "w")) #从内存中写入到文件
参考:
https://www.cnblogs.com/alex3714/articles/5161349.html
https://docs.python.org/3/library/configparser.html?highlight=configparser
configparser配置文件处理的更多相关文章
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- Python模块之: ConfigParser 配置文件读取
Python模块之: ConfigParser 配置文件读取 ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...
- configparser 配置文件模块
#_author:star#date:2019/11/7# configparser 配置文件模块import configparserconfig=configparser.ConfigParser ...
- logging 日志模块 configparser 配置文件
logging 模块 (copy博客) 详情浏览:http://www.cnblogs.com/linhaifeng/articles/6384466.html#_label12 函数式简单配置 im ...
- configparser配置文件操作
configparser 模块用于对配置操作 官方文档地址https://docs.python.org/3/library/configparser.html 导入configparser模块 i ...
- configparser配置文件模块
1.configparser的作用 mysql等很多文件的配置如下: [DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLeve ...
- day31 configparser 配置文件模块
#__author__: Administrator #__date__: 2018/8/8 # configparse 生成配置文件,配置文会以件.ini结尾 # 对于格式有要求 # 创建配置文档 ...
- python configparser配置文件解析器
一.Configparser 此模块提供实现基本配置语言的ConfigParser类,该语言提供类似于Microsoft Windows INI文件中的结构.我们经常会在一些软件安装目录下看到.ini ...
- hashlib摘要算法模块,logging日志,configparser配置文件模块
一.hashlib模块(摘要算法模块) 1.算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...
随机推荐
- php -- func_get_args
该方法必须在某个方法内部执行才有效 返回值为索引数组,一个数组元素对应一个参数
- ftrace:跟踪你的内核函数! | Linux 中国
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79161135 ftrace 是一个 L ...
- layui 笔记
弹出层 点击事件 <!DOCTYPE html> <html> <head> <title></title> {load href=&quo ...
- MYSQL 文件类型
首先, 表结构文件 : 1) *.frm是描述了表的结构, 数据及索引文件 如果是MyISAM引擎,则是 1) *.MYD保存了表的数据记录, 2) *.MYI则是表的索引 对于 InnoDB引擎,则 ...
- Transaction rolled back because it has been marked as rollback-only分析解决方法
1. Transaction rolled back because it has been marked as rollback-only事务已回滚,因为它被标记成了只回滚<prop key= ...
- 简单工厂模式(Java与Kotlin版)
Kotlin基础知识的学习,请参考之前的文章: Kotlin入门第一课:从对比Java开始 Kotlin入门第二课:集合操作 Kotlin入门第三课:数据类型 初次尝试用Kotlin实现Android ...
- iOS WKWebView (NSURLProtocol)拦截js、css,图片资源
项目地址github:<a href="https://github.com/LiuShuoyu/HybirdWKWebVIew/">HybirdWKWebVIew&l ...
- Xcode - 因为证书问题经常报的那些错
1.确认下证书是不是开发证书,如果是发布证书就会出现这样的提示. 2.证书失效了,去开发者中心重新生成一个. 3.包标识符不与描述文件包含的包标识符不一致,按照它的提示换一下就好了,最好不要点 Fix ...
- J Press the Button
BaoBao and DreamGrid are playing a game using a strange button. This button is attached to an LED li ...
- hello 2019 D
一开始sb了考虑总的因子疯狂T,做题太少了...没意识到会有辣么多因子... 神仙说1e9以内的最多的就有800个因子的了... 然后我们可以考虑质因子 我觉得已经说得很明白了... 唔逆元好像exg ...