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)

sections()方法返回可用的section,默认DEFAULT是不会返回的
import  configparser

config = configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
config.read(file)
username = config.sections()
print(username)

看效果需要自己新增一个section,Vuiki是我工作中配置文件本来就有的- - 
 
 

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模块的更多相关文章

  1. Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html

    Python3学习笔记(urllib模块的使用)   1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None,  ...

  2. Python3学习笔记(urllib模块的使用)

    转载地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None,  ...

  3. 【转】Python3学习笔记(urllib模块的使用)

    原文地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None,  ...

  4. python3学习笔记(7)_listComprehensions-列表生成式

    #python3 学习笔记17/07/11 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #通过列表生成式可以生成格式各样的list,这种list 一 ...

  5. python3学习笔记(6)_iteration

    #python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #类似 其他语言的for循环,但是比for抽象程度更高 # f ...

  6. Python3学习笔记 - 准备环境

    前言 最近乘着项目不忙想赶一波时髦学习一下Python3.由于正好学习了Docker,并深深迷上了Docker,所以必须趁热打铁的用它来创建我们的Python3的开发测试环境.Python3的中文教程 ...

  7. python3学习笔记(5)_slice

    #python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #切片slice 大大简化 对于指定索引的操作 fruits ...

  8. python3学习笔记(4)_function-参数

    #python学习笔记 17/07/10 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- import math #函数 函数的 定义 #定义一个求绝对值 ...

  9. python自动化测试学习笔记-5常用模块

    上一次学习了os模块,sys模块,json模块,random模块,string模块,time模块,hashlib模块,今天继续学习以下的常用模块: 1.datetime模块 2.pymysql模块(3 ...

随机推荐

  1. Spring JPA学习笔记

    目录 什么是JPA? 引入配置 新建一个Entity Bean类 JPA的增删改查 新建操作接口 新建测试类 总结 什么是JPA? 什么是JDBC知道吧?数据库有Mysql,SQL Server,Or ...

  2. Hadoop记录-HDFS balancer配置

    HDFS balancer配置(可通过CM配置)dfs.datanode.balance.max.concurrent.moves 并行移动的block数量,默认5 dfs.datanode.bala ...

  3. VirtualBox虚拟机中安装XP系统

    Windows XP是一款经典的操作系统,同时也是一款很老的操作系统,不过尽管如此,还是有一批用户在使用XP系统,所以发行一些软件的时候还是要测试在XP系统中能否运行,这时候我们就可以借助Virtua ...

  4. 很好的sql多表

    oracle 多表联合查询总结归纳 2018年08月16日 20:57:39 阅读数:44905 本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: ...

  5. spring整合ActiveMq

    spring整合ActiveMq: 1:依赖的jar包: 2:spring-activemq.xml    的配置: 代码: <?xml version="1.0" enco ...

  6. 09、 在QQ音乐中查找七里香这首歌的精彩评论

       找到七里香这首歌的精彩评论      URL https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk=5381&l ...

  7. Struts2的JSON插件

    扎心了,老铁~这依然是一个注册. 1.reg.jsp <%@page contentType="text/html; charset=utf-8"%> <!DOC ...

  8. linux 中 如何 搜索 指定目录 下 指定文件 的 指定内容

    开发时,经常遇到 全局查找某些代码 linux 中 如何 检索 某 目录下指定文件 的 指定内容如下: //.点为查找当前目录 下 的 所有 *.php 文件里 有 hello 的文件 find . ...

  9. IDAPython教程(三)

    在过去两个部分中,我们已经讨论了使用IDAPython让逆向工程更容易一些.这一部分我们来看一下条件断点. 当在IDA中调试时,分析者经常会遇到希望可以在一个特殊的地址中断下来的情况,但这只有在一些特 ...

  10. grep 打印相关行内容

    grep 打印相关行数常用参数: -r:关键字 -c:打印符合要求的行数 -i:忽略大小写 -n:输出行和行号 -v:打印不符合要求的行,即反选 -A:后跟数字(有无空格都可以),例如 -A3表示打印 ...