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. MySQL权限授权认证详解

    MySQL权限授权认证详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL权限系统介绍1>.权限系统的作用是授予来自某个主机的某个用户可以查询.插入.修改.删除 ...

  2. 6.Hystrix-超时设置

    由于客户端请求服务端方法时,服务端方法响应超过1秒将会触发降级,所以我们可以配置Hystrix默认的超时配置 如果我们没有配置默认的超时时间,Hystrix将取default_executionTim ...

  3. Kafka权威指南 读书笔记之(四)Kafka 消费者一一从 Kafka读取数据

    KafkaConsumer概念 消费者和消费者群组 Kafka 消费者从属于消费者群组.一个群组里的消费者订阅的是同一个主题,每个消费者接收主题一部分分区的消息. 往群组里增加消费者是横向伸缩消费能力 ...

  4. Linux记录-salt分析

    salt-master建立分组 如dn: 'L@dnxxx,dyyy' salt -N dn  state.apply  hadoop test=true salt -N dn  cmd.run  ' ...

  5. Contrast Ratio(Enhanced) (Level AAA)

    Contrast ratio between your text and background is at least 7:1 All of your users will benefit from ...

  6. termux 开启 sshd

    众所周知, termux 上的 sshd 不能通过 IP 连接, 只能使用密钥, 对于使用 PuTTY 的 Windows 用户, 怎么办呢? 由于 PuTTY 支持 telnet, 而 termux ...

  7. forEach 如何提前终止 跳出运行

    forEach 如何提前终止 跳出运行 try{ arr.forEach(function(item,index){ if (...) { foreach.break=new Error(" ...

  8. springboot(二十一):SpringBoot使用Mybatis注解开发教程-分页-动态sql

    https://blog.csdn.net/KingBoyWorld/article/details/78948304

  9. Centos下查看mysql的版本

    判断是否安装了mysql 输入 whereis mysql   如果安装了会显示mysql的安装所在路径 方法1:使用mysql -v命令查看: 1 2 3 4 [root@yeebian mysql ...

  10. 隐马尔可夫模型HMM(一)

    摘自 1.李航的<统计学习方法> 2.https://www.cnblogs.com/pinard/p/6945257.html 了解HMM模型 1.隐马尔可夫模型的定义 隐马尔可夫模型是 ...