python configparser读取库配置文件ini文件

#coding=utf-8
import configparser
cfg = configparser.ConfigParser()#先实例化
cfg.read("..\config\config.cfg",encoding='UTF-8')#指定配置文件的路径,和编码格式

.sections()#返回section列表。缺省section不包括在内。
print(cfg.sections())#获取所有section
#['english', 'french', 'files']

print(cfg["french"]["greeting"])#获取指定section下option的值
#Bonjour 字符串类型

print(cfg["french"])

print(cfg.items("french"))#获取指定section下所有键值对,以二元组形式返回
#[('greeting', 'Bonjour')]

.options(section)#返回section的所有option
print(cfg.options("french"))#获取指定section下的所有key
#['greeting']
"""
.add_section(section_name)#增加一个section。

.has_section(section_name)#判断section是否存在

.set(section,option,value) 给section项中写入键值对
 
.has_option(section, option)#判断section是否存在这个option

.get(section, option,*, raw=False, vars=None[, fallback])
#从指定的段的选项上取值,如果找到返回,如果没有找到就去找DEFAULT段有没有。
"""

#在对ini配置文件进行增加、修改、删除后要保存修改的话需要对配置文件进行写入
#with open (filename,"w") as f:
#	cfg.write(f)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值