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等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...
随机推荐
- Java分布式集群,使用synchronized和Redis保证Job的原子性
1.使用synchronized保证并发时,同时只会有一个请求执行该代码段: 2.在执行前先设置并获取Reids标记,先设置然后获取确保是否已经执行:SetOption.SET_IF_ABSENT 可 ...
- SpringCloud(一)浅谈SpringCloud
前言 现在微服务实在是太火了,所以我们必不可少的是要学习一下SpringCloud了,服务化的核心就是将传统的一站式应用 根据业务拆分成一个一个的服务,而微服务在这个基础上要更彻底地去耦合(不再共享D ...
- laravel5.8笔记七:语言包
语言包控制config/app.php 'locale' => 'en', 语言包位置:resources/lang/cn/ 建立resources/lang/cn/common.php < ...
- js复制文本内容到剪贴板
记录一下使用clipboardData复制不成功. 1.定义一个按钮执行复制 <div> <button type="button" id="copyR ...
- LINUX下从mysql文件导出后标题合并
这两天在做数据导出,真实折磨死了,记录下来.导出的格式是csv. 由于我们的数据量比较大,导出到excel时,几百万上千万行的时候用程序去写入肯定是不行,所以自然就想到了mysql的outfile功能 ...
- 终端命令行编译运行C#程序HelloWorld
终端命令行编译运行C#程序HelloWorld 今天忽然有人问我,刚学习java程序的时候,一般老师都会建议我们从配置环境变量开始从命令行编译运行程序,那么学习c#的时候基本是直接安装visual s ...
- 2018年总结&2019年计划
今天加了来自博客园的好友,才意识到博客已经荒废了许久.其实自己最近也是考虑了总结一下这个繁忙的2018. 2019计划: . 多给家里通电话: . 关心照顾好小奶糖: . 多读书,非小说: . 坚持吃 ...
- 松散的css
<style> <p>hello world</p> {} p { color: red; } {} <em style="padding: 4px ...
- Timer和时间调度
Timer作为JDK提供的util工具,不太适合作为周期调度任务,只适合简单的定时操作(按照一定时间频率出发任务),在java的领域解决方案中,Quartz无疑是翘楚. Timer的调度方法有: pu ...
- 微信小程序之wx.showmodal
1. . wx.showModal({ title: "2222步", content: currentCache ? "确定为自己城市添加步数吗" : &qu ...