Python configparser模块操作代码实例


Posted in Python onJune 08, 2020

1、生成配置文件

''' 
  生成配置文件
'''
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 相关文章推荐
深入探究Python中变量的拷贝和作用域问题
May 05 Python
Pycharm学习教程(2) 代码风格
May 02 Python
python在每个字符后添加空格的实例
May 07 Python
PyTorch的Optimizer训练工具的实现
Aug 18 Python
Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]
Feb 13 Python
如何使用pandas读取txt文件中指定的列(有无标题)
Mar 05 Python
keras 自定义loss层+接受输入实例
Jun 28 Python
PyCharm2020.1.1与Python3.7.7的安装教程图文详解
Aug 07 Python
python3获取控制台输入的数据的具体实例
Aug 16 Python
PyCharm 解决找不到新打开项目的窗口问题
Jan 15 Python
python推导式的使用方法实例
Feb 28 Python
pytorch交叉熵损失函数的weight参数的使用
May 24 Python
pycharm 实现本地写代码,服务器运行的操作
Jun 08 #Python
pycharm软件实现设置自动保存操作
Jun 08 #Python
Python decimal模块使用方法详解
Jun 08 #Python
深入了解python列表(LIST)
Jun 08 #Python
将pycharm配置为matlab或者spyder的用法说明
Jun 08 #Python
基于python实现matlab filter函数过程详解
Jun 08 #Python
浅谈tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意点
Jun 08 #Python
You might like
浅析PHP安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
2013/07/05 PHP
基于PHP实现假装商品限时抢购繁忙的效果
2015/10/16 PHP
PDO实现学生管理系统
2020/03/21 PHP
jQuery 动画基础教程
2008/12/25 Javascript
深入理解JavaScript系列(7) S.O.L.I.D五大原则之开闭原则OCP
2012/01/15 Javascript
有关于JS辅助函数inherit()的问题
2013/04/07 Javascript
Javascript控制页面链接在新窗口打开具体方法
2013/08/16 Javascript
用jquery修复在iframe下的页面锚点失效问题
2014/08/22 Javascript
JavaScript函数柯里化原理与用法分析
2017/03/31 Javascript
详解vue-cli中配置sass
2017/06/21 Javascript
vue2.0 datepicker使用方法
2018/02/04 Javascript
vue-router跳转时打开新页面的两种方法
2019/07/29 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
解决基于 keep-alive 的后台多级路由缓存问题
2020/12/23 Javascript
Python中使用PIPE操作Linux管道
2015/02/04 Python
Python中的集合类型知识讲解
2015/08/19 Python
python下如何查询CS反恐精英的服务器信息
2017/01/17 Python
Python之Scrapy爬虫框架安装及使用详解
2017/11/16 Python
widows下安装pycurl并利用pycurl请求https地址的方法
2018/10/15 Python
Python中的枚举类型示例介绍
2019/01/09 Python
python字符串Intern机制详解
2019/07/01 Python
使用Tensorflow实现可视化中间层和卷积层
2020/01/24 Python
如何对python的字典进行排序
2020/06/19 Python
python 还原梯度下降算法实现一维线性回归
2020/10/22 Python
一款基于css3和jquery实现的动画显示弹出层按钮教程
2015/01/04 HTML / CSS
html5小程序飞入购物车(抛物线绘制运动轨迹点)
2020/10/19 HTML / CSS
英语专业大学生求职简历的自我评价
2013/10/18 职场文书
劳资人员岗位职责
2013/12/19 职场文书
创业计划书怎样才能打动风投
2014/01/01 职场文书
中学家长会邀请函
2014/01/17 职场文书
医药学专业大学生职业生涯规划书论文
2014/01/21 职场文书
幼儿园门卫岗位职责
2014/02/14 职场文书
餐饮总经理岗位职责
2014/03/07 职场文书
协议书怎么写
2014/04/21 职场文书
论群众路线学习心得体会
2014/10/31 职场文书
MySQL数据库必备之条件查询语句
2021/10/15 MySQL