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 ...
随机推荐
- SpringBoot实战之异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- 设置程序PrivatePath,配置引用程序集的路径(分离exe和dll)
原文:设置程序PrivatePath,配置引用程序集的路径(分离exe和dll) 有时候我们想让程序的exe文件和dll文件分开在不同目录,这时候可以有3种方法 1.在app.config中配置 &l ...
- delete records in table A not in table B
转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低 ...
- 链表源代码(C语言实现)
源代码(C语言实现) ①.构造链表节点 typedef struct Node //一个单独的节点 { int ...
- Hibernate→ 《Hibernate程序开发》教材大纲
Hibernate ORM 概览 Hibernate 简介 Hibernate 架构 Hibernate 环境 Hibernate 配置 Hibernate 会话 Hibernate 持久化类 Hib ...
- java8的stream系列教程之filter过滤集合的一些属性
贴代码 List<Student> lists = new ArrayList<>(); Student student = new Student(); student.se ...
- Directx教程(30) 如何保证渲染物体不会变形
原文:Directx教程(30) 如何保证渲染物体不会变形 在Directx11教程(6)中, 我们曾经实现过这个功能,但那时是在SystemClass中,处理WM_SIZE时候,重新调用m ...
- PHPCMS快速建站系列之添加单页模版
单页模板命名:page_xxx.html 以page_开头 在模版所在目录的config.php中添加配置项 'page_xxx.html' => '单网页', 也可以不在config中配置,不 ...
- C++模板相关知识点总结
1:在 C++ 中,模板是泛型编程的基础.模板是创建类或函数的蓝图或公式. 2:模板定义以关键字 template 开始,后接模板形参表,模板形参表是用尖括号括住的一个或多个模板形参的列表,形参之间以 ...
- Java练习 SDUT-1119_输入数字星期,输出英文(switch语句)
C语言实验--输入数字星期,输出英文(switch语句) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 从键盘上输入数 ...