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 ...
随机推荐
- Python package下载中遇到ReadTimeoutError: HTTPSConnectionPool?
问题描述: Python package下载中遇到ReadTimeoutError: HTTPSConnectionPool? 问题解决: 方法1:继续重复下载 pip install virtual ...
- ThinkPHP3.2中字段unique验证出错的解决方法
protected $_validate=array( array('stu_id','','学号已存在',1,'unique',1), ) 当一次插入多条数据时: 在进行循环 使用create验证时 ...
- ThreadPoolExecutor线程池详解
七个参数: corePoolSize:核心池的大小,在创建了线程池后,默认情况下,线程池中并没有任何线程,而是等待有任务到来才创建线程去执行任务,当有任务来之后,就会创建一个线程去执行任务,当线程池中 ...
- 解决CDN传统方法引入Iview icon 不显示问题
因为需要字体文件,可以在github上下载. 将文件下载之后放到fonts文件夹下,fonts文件夹要与Iview.css在同级目录
- sql递归查询 根据Id查所有子结点
Declare @Id Int Set @Id = 0; ---在此修改父节点 With RootNodeCTE(D_ID,D_FatherID,D_Name,lv) As ( Select D_ID ...
- cocos-2dx 3.17 环境搭建 与 新建工程
由于上次在ubuntu中安装了cocos2dx的时候环境被破坏了,所以本文是基于 Windows 编写的 平台: OS : windows 10 compiler : VS 2015 下载 coc ...
- docker 系列 - 基础镜像环境和Docker常用命令整理
=======================docker 基础镜像环境 alpine=======================可以使用 docker search 命令搜索指定的 image, ...
- MongoDB 更新数组中的元素
本文记录如何更新MongoDB Collection 中的Array 中的元素.假设Collection中一条记录格式如下: 现要删除scores 数组中,"type" 为 &qu ...
- nmap学习之nmap -sP 【目标】
一.通过arp包判断局域网内的主机状态 二.对于局域网外的主机通过向主机 1)发送普通ICMP请求包[类型字段为8,代码字段为0]: 2)发送时间戳ICMP请求包[类型字段为13,代码字段为0]: 3 ...
- AD软件使用心得
1.在更新原理图之前一定要标记所有器件,否则无法生成PCB器件. 2.学会用sch list网表来批量修改器件名称 3.布线的面