configparser

获取所有节点

import configparser

config=configparser.ConfigParser

config.read('',encoding='utf-8')

ret=config.sections()

print(ret)

//获取指定节点下所有的键值对

import configparser

config=configparser.ConfigParser()

config.read('xxx',encoding='utf-8')

ret=config.items('section1')//注意items是获取到所有的键值对

print(ret)

//获取指定节点下所有的键

import configparser

config=configparser.ConfigParser

config.read('xxxxx',encoding='utf-8')

ret=config.options('section1')

print(ret)

//获取指定节点下指定key的值:

import configparser

config=configparser.ConfigParser()

config.read("xxx",encoding='utf-8')

v=config.get('section1','k1')//利用get可以获取指定节点下指定的key值。

print(v)

注意在这个里边是不需要写双引号的,因为默认的就是带了双引号的。注意这里还有可以

v=config.getint('section1','k1')

v=config.getfloat('section1','k1')

v=config.getboolean('section1','k1')

print(v)

检查:

has_sec=config.has_section('section1')

print(has_sec)

添加节点

config.add_section("SEC_1")

config.write(open('xxxooo','w'))

删除节点

config.remove_section("SEC_1")

config.write(open('xxxooo','w'))

has_opt=config.has_option('section1','k1')

print(has_opt)

//删除

config.remove_option('section1','k1')

config.write(open('xxxooo','w'))

//设置

config.set('section1','k10',"123")

config.write(open('xxxooo','w'))

 

posted @ 2017-02-26 15:37  关泉珍  阅读(39)  评论(0)    收藏  举报