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等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...
 
随机推荐
- [python] python3.6  安装 pytesseract 出错
			
安装pytesseact出错, 下载 tesseract-ocr , 地址 https://github.com/tesseract-ocr/tesseract 修改pytesseract.py 设置 ...
 - 定时 清理 elasticsearch 6.5.4 的 索引 文件
			
#!/bin/sh ip='127.0.0.1' port=' day=`date -d "2 days ago" +%Y%m%d` #day=`date -d "5 h ...
 - iOS企业版打包 发布在线安装包 plist
			
本文转载至 http://blog.csdn.net/u011452278/article/details/49511385 原文转载:http://blog.csdn.net/pang040328/ ...
 - 微信小程序-bug-调用wx.login()无响应的原因和解决方案
			
想必,最近有些小程序开发者,在调用wx.login()的时候,在部分IOS上无响应的情况: 补充一点:在测试模式,调试模式,都是OK的,一上正式环境就GG了,百思不得其解啊! 响应结果如下: 在suc ...
 - Linux网络设备驱动 _驱动模型
			
Linux素来以其强大的网络功能著名,同时, 网络设备也作为三大设备之一, 成为Linux驱动学习中必不可少的设备类型, 此外, 由于历史原因, Linux并没有强制对网络设备贯彻其"一切皆 ...
 - 【CF613D】Kingdom and its Cities 虚树+树形DP
			
[CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...
 - 11.14 luffycity项目(6)
			
2018-11-14 21:26:45 实现了购物车功能! 涉及到了redis的使用 需要在pycharm中下载 django_redis 其他的看一下笔记,有购物车里面数据存储的结构才发现数据 ...
 - nginx配置-为没有后缀的文件(实际上是有html文件)以html形式打开
			
location ~ index.php@ { add_header content-type "text/html"; }
 - dubbo+zookeeper+spring实例
			
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
 - java爬取网站信息和url实例
			
https://blog.csdn.net/weixin_38409425/article/details/78616688(出自此為博主) 具體代碼如下: import java.io.Buffer ...