Python configparser模块操作代码实例
1、生成配置文件
'''
生成配置文件
很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:1097524789
'''
import configparser config = configparser.ConfigParser() # 初始化赋值
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
# 追加
config['DEFAULT']['ForwardX11'] = 'yes' 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 with open('example.ini', 'w') as configfile:
config.write(configfile)
2、读取配置文件
# 读
import configparser
config = configparser.ConfigParser()
config.sections()
config.read('example.ini')
# {'serveraliveinterval': '45', 'compression': 'yes', 'compressionlevel': '9', 'forwardx11': 'yes'}
print(config.defaults()) # hg
print(config['bitbucket.org']["User"]) # 50022
print(config["topsecret.server.com"]["host port"])
3、删除
# 删除(创建一个新文件,并删除 bitbucket.org)
import configparser
config = configparser.ConfigParser()
config.sections() config.read('example.ini')
rec = config.remove_section("bitbucket.org") # 删除该项
config.write(open("example.cfg","w"))
生成新文件 example.cfg
DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes topsecret.server.com]
host port = 50022
forwardx11 = no
删除,并覆盖原文件
# 删除(删除 bitbucket.org)
import configparser
config = configparser.ConfigParser()
config.sections() config.read('example.ini')
rec = config.remove_section("bitbucket.org") # 删除该项
config.write(open("example.ini","w"))
4、修改
import configparser
config = configparser.ConfigParser()
config.read('example.ini') #读文件
config.add_section('yuan') #添加section
config.remove_section('bitbucket.org') #删除section
config.remove_option('topsecret.server.com',"forwardx11") #删除一个配置项
config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222')
with open('new2.ini','w') as f:
config.write(f)
生成新文件 new2.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [topsecret.server.com]
host port = 50022
k1 = 11111 [yuan]
k2 = 22222
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们
Python configparser模块操作代码实例的更多相关文章
- python自动化--模块操作之re、MySQL、Excel
一.python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("www ...
- Python中类的继承代码实例
Python中类的继承代码实例 这篇文章主要介绍了Python中类的继承代码实例,本文直接给出代码及运行效果,需要的朋友可以参考下 相对于C 的继承编写,Python更简洁,而且效率也是很高的,下面编 ...
- python -ConfigParser模块讲解
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- Python - configParser模块学习
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- 【python】python configparser模块
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...
- python ConfigParser模块 配置文件解析
ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...
- python ConfigParser 模块
ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...
随机推荐
- 机器学习实战基础(十):sklearn中的数据预处理和特征工程(三) 数据预处理 Preprocessing & Impute 之 缺失值
缺失值 机器学习和数据挖掘中所使用的数据,永远不可能是完美的.很多特征,对于分析和建模来说意义非凡,但对于实际收集数据的人却不是如此,因此数据挖掘之中,常常会有重要的字段缺失值很多,但又不能舍弃字段的 ...
- 移除hmtl5 input type=“number”的step验证
设置 step="any"例如:<input type="number" step="any" value=""& ...
- 手把手从零开始---封装一个vue视频播放器组件
现在,在网页上播放视频已经越来越流行,但是网上的资料鱼龙混杂,很难找到自己想要的,今天小编就自己的亲身开发体验,手把手从零开始---封装一个vue视频播放器组件. 作为一个老道的前端搬砖师,怎么可能会 ...
- 项目管理:如何显性管理并提升Story分解能力
引言: 在“DevOps能力之屋(CapabilitiesHouse of DevOps)”中,华为云DevCloud提出(工程方法+最佳实践+生态)×工具平台=DevOps能力.华为云DevClou ...
- noi-vim配置
colorscheme ron set t_Co=256 set mouse=a set ts=4 set sw=4 set nu set ru set ai set ci set cin set l ...
- 【JUnit测试】总结
什么是Junit? Junit是xUnit的一个子集,在c++,paython,java语言中测试框架的名字都不相同 xUnit是一套基于测试驱动开发的测试框架 其中的断言机制:将程序预期的结果与程序 ...
- 016.Nginx HTTPS
一 HTTPS概述 1.1 HTTPS介绍 超文本传输安全协议HTTPS(Hypertext Transfer Protocol Secure)是超文本传输协议和SSL/TLS的组合,用以提供加密通讯 ...
- 如何证明sleep不释放锁,而wait释放锁?
wait 加锁示例 public class WaitDemo { private static Object locker = new Object(); public static void ma ...
- Monster Audio 使用教程 (五) 添加区域效果器
我们可以在音轨上,某一个时间区域内,添加一组效果器,这组效果器,只有在播放指针进入它的区域时,效果器才可以处理声音 首先,先在时间刻度上,设定好时间范围 然后,在音轨的波形区域点击右键,然后点击[添加 ...
- 新阿里云服务器从0开始配置为python开发环境
由于每次打开linux虚拟机比较麻烦,于是尝试一下云服务器,在阿里云领取了一个月的试用服务器,这里记录一下新服务器从0配置成python开发环境的步骤,以便以后配置新服务器时有个参考. 免费领取一个月 ...