python模块之configparser模块
configparser模块:用于按一定格式创建配置文件
创建
import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'default': 'yes'}
config['path'] = {'userinfo': r'E:\pycharm\学习\day29\userinfo'}
with open('userinfo.ini', 'w', encoding='utf-8') as f:
config.write(f)
完成后的文件:
[DEFAULT]
default = yes [path]
userinfo = E:\pycharm\学习\day29\userinfo
查看
print(config.sections()) # [] 因为还没有读取文件
config.read('userinfo.ini', encoding='utf-8')
print(config.sections()) # ['path'] 读出节点 ['DEFAULT']为默认节点 不打印
print('path' in config) # True 判断某节点是否在配置文件中
print(config['path']['userinfo']) # E:\pycharm\学习\day29\userinfo 读取节点下的配置项 没有该项目标报错
print(config.get('path', 'userinfo')) # E:\pycharm\学习\day29\userinfo for k in config['path']: # 打印'path'节点下的配置项的同时还会打印默认节点下的所有项
print(k) # userinfo default print(config.items('path')) # [('default', 'yes'), ('userinfo', 'E:\\pycharm\\学习\\day29\\userinfo')]
增加
config.read('userinfo.ini', encoding='utf-8')
config.add_section('IP') # 增加节点
print(config.sections()) # ['path', 'IP']
config.set('IP', 'ip', '192.168.1.1') # 给节点增加配置项
config.set('path', 'userinfo', 'None') # 修改配置项
print(config['IP']['ip']) # 192.168.1.1
print(config['path']['userinfo']) # None
config.write(open('userinfo.ini', 'w', encoding='utf-8')) # 将修改重新写回文件
删除
config.read('userinfo.ini', encoding='utf-8')
print(config.sections()) # ['path', 'IP']
config.remove_section('IP') # 删除节点
print(config.sections()) # ['path']
print(config.items('path')) # [('default', 'yes'), ('userinfo', 'None')]
config.remove_option('path', 'userinfo') # 删除节点中的配置项
print(config.items('path')) # [('default', 'yes')]
config.write(open('userinfo.ini', 'w', encoding='utf-8')) # 将修改重新写回文件
python模块之configparser模块的更多相关文章
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- python:利用configparser模块读写配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- python基础14 ---函数模块4(configparser模块)
configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- 《Python》hashlib模块、configparser模块、logging模块
一.hashlib模块 Python的hashlib模块中提供了常见的摘要算法,如md5,sha1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的字符串(通 ...
- Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块
一.shutil 模块 1.shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,需要打开文件 import shutil shutil.co ...
随机推荐
- 学习String类
1. 描述: String类是java中比较常用的类, 表示字符串类型 当拼接大量数据时, String类性能没有StringBuilder和StringBuffer性能高 2. 常用的String语 ...
- 2-4 Numpy+Matplotlib可视化(二)
自定义绘图 # -*-coding:utf-8-*- # !/usr/bin/env python # Author:@vilicute import numpy as np import matpl ...
- 使用 store 来优化 React 组件
在使用 React 编写组件的时候,我们常常会碰到两个不同的组件之间需要共享状态情况,而通常的做法就是提升状态到父组件.但是这样做会有一个问题,就是尽管只有两个组件需要这个状态,但是因为把状态提到了父 ...
- NOIP模拟 17.8.20
NOIP模拟17.8.20 A.阶乘[题目描述]亲爱的xyx同学正在研究数学与阶乘的关系,但是他喜欢颓废,于是他就制作了一个和阶乘有关系的数学游戏:给出两个整数 n,m,令 t = !n,每轮游戏的流 ...
- 在Liferay 7中如何自定义一个Portlet的toolbar
哈哈,懒得自己写了,直接贴教程了,你想为那个portlet添加自定义的toolbar,就在javax.portlet.name=属性中写上它的值.教程博客:Adding Portlet URL in ...
- This Product is covered by one or more of the folloWing patents
借用一下网络图片,作为描述: 原因: 启动方式使用了网络启动, 解决方案: 进入bios,修改启动方式,禁用网卡驱动,使用从硬盘启动或者从U盘启动即可.
- jdbc连接数据库的步骤(转载)
http://hzy3774.iteye.com/blog/1689525 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过jav ...
- 使用VirtualBox + Vagrant打造统一的开发环境
https://blog.csdn.net/openn/article/details/54927375 配置步骤 安装VirtualBox 虚拟系统运行在VirtualBox中,类似的工具还有VMw ...
- iOS 三种打电话方式
//1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 NSMutableString * str=[[NSMutableString alloc] initWit ...
- php7 新内容
1.use增强 以thinkphp5.0为例 namespace app\home\controller;use think\{Loader,Controller,Captcha,Request}; ...