configparser模块

echo   $@ $# $? $*
configparser
configparse用于处理特定格式的文件,其本质上利用open来操作文件(比如配置文件)
**********配置文件***************
#注释1这个一个配置文件
[secton1] #节点
k1 = v1 #值
k2:v2  #值
[section2] #节点
k1 = v2#值


*********************************
@1)、获取所有节点

import configparser
config = configparser.ConfigParser()
config.read('xxooo.txt', encoding='utf-8')
ret = config.sections()
print(ret)
 


@2)、获取指定节点下所有的键值对
import configparser
config = configparser.ConfigParse()
config.read('xxoo.txt', encoding='utf-8')
ret = config.items('sections')
print(ret)


@3)、获取指定节点下所有的键
import configparser
config = configparser.ConfigParser()
config.read("xxoo.txt", encoding="utf-8")
ret = config.options('section1')
print(ret)


@4)、获取指定节点下指定key值
import configparser
config = configparser.ConfigParser()
config.read('xxoo.txt', encoding='utf-8')
v = config.get('section1', 'k1')
#v = config.getint('section1', 'k1')
#v = config.getfloat('section1', 'k1')
#v = config.getboolean('section1', 'k1')
print(v)


@5)、检查、删除、添加节点
import configparser
config = configparser.ConfigParser()
config.read('xxoo.txt', encoding='utf-8')
#检查
has_sec = config.has_section('section1')
print(has_sec)
#添加节点
config.add_section('SEC_1')
config.write(open('xxoo.txt', 'w'))
#删除节点
config.remove_section("SEC_1")
config.write(open("xxoo.txt", 'w'))


@6)、检查、删除、设置指定组内的键值对
import configparser
config = configparser.ConfigParser()
confgi.read('xxoo.txt', encoding='utf-8')
#检查
has_opt = config.has_option('section1','k1')
print(has_opt)
#删除
config.remove_option('section1', 'k1')
config.write(open('xxoo.txt','w'))
#设置
config.set('section1','k10','123')
config.write(open("xxoo.txt",'w'))



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值