Python3学习笔记27-ConfigParser模块

摘要:
ConfigParser模块在Python3修改为configparser,这个模块定义了一个ConfigeParser类,该类的作用是让配置文件生效。

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下的某个键值对

importconfigparser

config =configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'config.read(file)
config.add_section('login')
config.set('login','username','1111')
config.set('login','password','2222')
with open(file,'w') as configfile:
    config.write(configfile)

Python3学习笔记27-ConfigParser模块第1张

read()方法是用来读取配置文件的,如果不加上read()方法,写入是直接从头开始写的,使用了read()之后,是从读取完后的光标开始写入,类似追加模式'a'一样。可能有疑惑,既然有追加模式,直接把with里面的'w'换成'a'就可以,干嘛还要读一次

将上面的代码修改一下,将username和password的值修改一下

importconfigparser

config =configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'config.read(file)
config.set('login','username','2222')
config.set('login','password','3333')
with open(file,'w') as configfile:
    config.write(configfile)

Python3学习笔记27-ConfigParser模块第2张

会发现username和password的值被修改了,如果使用'a'模式,会发现报错,没有找到login这个section。

就算把上面代码中加上config.add_section('login')也只会在后面进行追加新增,而不会做修改操作

所以考虑到把方法封装的缘故,使用read()和'w'写入模式,来实现追加新增,修改配置文件

读取

使用get()方法可以获得指定section下某个键的值

importconfigparser

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)

Python3学习笔记27-ConfigParser模块第3张

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

config =configparser.ConfigParser()
file = 'D:/PycharmProjects/Vuiki/Config/config.ini'config.read(file)
username =config.sections()
print(username)
Python3学习笔记27-ConfigParser模块第4张

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

options()返回对应section下可用的键

importconfigparser

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

Python3学习笔记27-ConfigParser模块第5张

has_section()方法判断section是否存在,存在返回True,不存在返回False

importconfigparser

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)

Python3学习笔记27-ConfigParser模块第6张

has_option()方法判断section下,某个键是否存在,存在返回True,不存在返回False

importconfigparser

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)

Python3学习笔记27-ConfigParser模块第7张

还有一些不罗列了,感兴趣可以去查一下

删除

remove_section()方法删除某个section

remove_option()方法删除某个section下的键

importconfigparser

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模块》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇layui给select下拉框赋值C#打印机操作类下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python3学习笔记11(函数)

函数 python提供了许多内建函数,例如print()。 自己创建的函数,叫做用户自定义函数。 定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以def关键词开头,后接函数标识符名称和圆括号()。 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数。 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。 函数内容以...

内网域安全入侵感知系统watchAD

一、前言介绍 WatchAD收集所有域控上的事件日志和kerberos流量,通过特征匹配、Kerberos协议分析、历史行为、敏感操作和蜜罐账户等方式来检测各种已知与未知威胁,功能覆盖了大部分目前的常见内网域渗透手法。该项目在360内部上线运行半年有余,发现多起威胁活动,取得了较好的效果。现决定开源系统中基于事件日志的检测部分。 目前支持的具体检测功能如下...

JedisPool无法获得资源问题

线上碰到一个问题:redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:22) 线上会相隔不定时的天数后出现一次Jedis...

python3之字符串(str)

1 命名 str='value' str="value" 2 原始字符串 str=r'value' value按照字面的意思使用,没有转义特殊或不能打印的字符 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法 3 转义字符 (在行尾时) 续行符 \...

angular 依赖注入

依赖注入    依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题。关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/articles/injection.html )。    Angular负责创建组件,解决它们之间的依赖关系,并按要求提供其他组件。 使用依赖注入   依赖注入在...

ubuntu1.8安装python3.7pip报错“subprocess.CalledProcessError...lsb_release”

背景 给一台 ubuntu18.04 安装 python3.7,完成后想试试pip list命令,结果出现一长串报错,我贴一下最后几行: File "/usr/local/lib/python3.7/subprocess.py", line 512, in runoutput=stdout, stderr=stderr)subprocess.CalledP...