Python3学习笔记27-ConfigParser模块
ConfigParser模块在Python3修改为configparser,这个模块定义了一个ConfigeParser类,该类的作用是让配置文件生效。配置文件的格式和window的ini文件相同,大致如下:
【section】
name = value
name:value
用 = 或 : 来赋值
section可以理解为一个模块,比如登录的时候,这个section可以叫login,下面放着username和password
该模块主要使用模块中RawConfigParser(),ConfigParser()、SafeConfigParse()这三个方法(三选一),创建一个对象使用对象的方法对配置文件进行增删改查操作
主要介绍ConfigParser()下的方法
涉及增删改的操作 都需要使用write()方法之后才会生效
add_section():用来新增section
set():用来新增对应section下的某个键值对
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
config.add_section('login')
config.set('login','username','')
config.set('login','password','')
with open(file,'w') as configfile:
config.write(configfile)
read()方法是用来读取配置文件的,如果不加上read()方法,写入是直接从头开始写的,使用了read()之后,是从读取完后的光标开始写入,类似追加模式'a'一样。可能有疑惑,既然有追加模式,直接把with里面的'w'换成'a'就可以,干嘛还要读一次
将上面的代码修改一下,将username和password的值修改一下
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
config.set('login','username','')
config.set('login','password','')
with open(file,'w') as configfile:
config.write(configfile)
会发现username和password的值被修改了,如果使用'a'模式,会发现报错,没有找到login这个section。
就算把上面代码中加上config.add_section('login')也只会在后面进行追加新增,而不会做修改操作
所以考虑到把方法封装的缘故,使用read()和'w'写入模式,来实现追加新增,修改配置文件
读取
使用get()方法可以获得指定section下某个键的值
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
username = config.get('login','username')
password = config.get('login','password')
print(username,password)
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
username = config.sections()
print(username)
options()返回对应section下可用的键
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
username = config.options('login')
print(username)
has_section()方法判断section是否存在,存在返回True,不存在返回False
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
test1 = config.has_section('login')
test2 = config.has_section('test')
print(test1)
print(test2)
has_option()方法判断section下,某个键是否存在,存在返回True,不存在返回False
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
test1 = config.has_option('login','username')
test2 = config.has_option('login','pass')
print(test1)
print(test2)
还有一些不罗列了,感兴趣可以去查一下
删除
remove_section()方法删除某个section
remove_option()方法删除某个section下的键
import configparser config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
config.remove_option('login','username')
config.remove_option('login','password')
config.remove_section('login')
with open(file,'w') as configfile:
config.write(configfile)
一定要先read()到内存,不然删除报错
Python3学习笔记27-ConfigParser模块的更多相关文章
- Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html
Python3学习笔记(urllib模块的使用) 1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, ...
- Python3学习笔记(urllib模块的使用)
转载地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None, ...
- 【转】Python3学习笔记(urllib模块的使用)
原文地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None, ...
- python3学习笔记(7)_listComprehensions-列表生成式
#python3 学习笔记17/07/11 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #通过列表生成式可以生成格式各样的list,这种list 一 ...
- python3学习笔记(6)_iteration
#python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #类似 其他语言的for循环,但是比for抽象程度更高 # f ...
- Python3学习笔记 - 准备环境
前言 最近乘着项目不忙想赶一波时髦学习一下Python3.由于正好学习了Docker,并深深迷上了Docker,所以必须趁热打铁的用它来创建我们的Python3的开发测试环境.Python3的中文教程 ...
- python3学习笔记(5)_slice
#python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #切片slice 大大简化 对于指定索引的操作 fruits ...
- python3学习笔记(4)_function-参数
#python学习笔记 17/07/10 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- import math #函数 函数的 定义 #定义一个求绝对值 ...
- python自动化测试学习笔记-5常用模块
上一次学习了os模块,sys模块,json模块,random模块,string模块,time模块,hashlib模块,今天继续学习以下的常用模块: 1.datetime模块 2.pymysql模块(3 ...
随机推荐
- java io系列11之 FilterOutputStream
FilterOutputStream 介绍 FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”.它主要包括BufferedOutputStream, Dat ...
- layui(七)——rate组件常见用法总结
layui中提供了rate组件,用法很简单,直接上代码. <div id="test1"></div> <script> layui.use(' ...
- Ruby on Rails,一对多关联(One-to-Many)
在上一篇文章中,我们知道通过has_one和belongs_to方法定义一对一关联关系.接下来是更常见的情况,一对多关联.比如老师与所教课程的关系,一个老师负责多个课程.换成对象之间的关系就是:一个老 ...
- Quartus II 中 Verilog 常见警告/错误汇总
Verilog 常见错误汇总 1.Found clock-sensitive change during active clock edge at time <time> on regis ...
- canvas高级篇(转载)移动元素
本文转载在http://bbs.blueidea.com/thread-2979405-1-1.html 哈哈哈,好骚气!终于解决了我的需求.可以移动canvas内的多个元素 <!DOCTYPE ...
- 微信、支付宝支付SDK
1.首先是下载SDK,其对应的SDK在mvn上下载不了,需要手动配置到仓库 支付宝SDK下载地址 https://docs.open.alipay.com/54/103419 微信SDK官方下载地址 ...
- springcloud 服务调用的两种方式
spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign.Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了rib ...
- 使用sort函数进行排序
介绍 C++的一个重要组成部分STL(Standard Template Library),即标准模板库,是一些高级数据结构和算法的集合:高级数据结构(容器)主要包括list.set.vector.m ...
- Spring boot 连接Redis实现HMSET操作
这篇文章记录使用spring-boot-starter-redis访问Redis.Redis相关的的配置文件放在Resources目录下的application.yml文件中,如下所示: spring ...
- IIS7.5配置过程
1.Windows功能,注意选择应用程序开发功能,否则不能使用经典模式. 2.Cmd运行(使用.netframework4.0)C:\Windows\Microsoft.NET\Framework\V ...