首页 > 编程语言 > 详细

python 模块2 类初步

时间:2016-06-25 12:12:36      阅读:164      评论:0      收藏:0      [点我收藏+]

模块

一、configparser

[section1]

k1 = v1
k1:v1

[section2]

k3 = v3

 

1、获取所有节点

import configparser
 
config = configparser.ConfigParser()
config.read(‘settings‘, encoding=‘utf-8‘)
res = config.sections()
print(res)

 

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

import configparser
 
config = configparser.ConfigParser()
config.read(‘settings‘, encoding=‘utf-8‘)
res = config.items(‘section1‘)
print(res)

 

3、获取节点下所有的键

import configparser
 
config = configparser.ConfigParser()
config.read(‘settings‘, encoding=‘utf-8‘)
res = config.options(‘section1‘)
print(res)

 

4、获取指定节点下指定key的值

import configparser
 
config = configparser.ConfigParser()
config.read(‘settings‘, 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(‘settings‘, encoding=‘utf-8‘)
 
 
# 检查
has_sec = config.has_section(‘section1‘)
print(has_sec)
 
# 添加节点
config.add_section("SEC_1")
config.write(open(‘settings‘, ‘w‘))
 
# 删除节点
config.remove_section("SEC_1")
config.write(open(‘settings‘, ‘w‘))

 

6、检查、删除、设置指定组内的键值对

import configparser
 
config = configparser.ConfigParser()
config.read(‘settings‘, encoding=‘utf-8‘)
 
# 检查
has_opt = config.has_option(‘section1‘, ‘k1‘)
print(has_opt)
 
# 删除
config.remove_option(‘section1‘, ‘k1‘)
config.write(open(‘settings‘, ‘w‘))
 
# 设置
config.set(‘section1‘, ‘k10‘, "123")
config.write(open(‘settings‘, ‘w‘))

 

python 模块2 类初步

原文:http://www.cnblogs.com/9527chu/p/5616038.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!