python configparser模块_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下的某个键值对

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)

70

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)

70

会发现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)

70

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

importconfigparser

config=configparser.ConfigParser()

file= 'D:/PycharmProjects/Vuiki/Config/config.ini'config.read(file)

username=config.sections()print(username)

70

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

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

importconfigparser

config=configparser.ConfigParser()

file= 'D:/PycharmProjects/Vuiki/Config/config.ini'config.read(file)

username= config.options('login')print(username)

70

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)

70

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)

70

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

删除

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()到内存,不然删除报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值