configparser模块
2016-06-01 09:52
381 查看
echo $@ $# $? $*
configparser
configparse用于处理特定格式的文件,其本质上利用open来操作文件(比如配置文件)
**********配置文件***************
#注释1这个一个配置文件
*********************************
@1)、获取所有节点
@2)、获取指定节点下所有的键值对
@3)、获取指定节点下所有的键
@4)、获取指定节点下指定key值
@5)、检查、删除、添加节点
@6)、检查、删除、设置指定组内的键值对
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'))
相关文章推荐
- SQL Server2008如何设置开启远程连接http://jingyan.baidu.com/article/fec4bce226a264f2618d8ba5.html
- java多线程状态切换
- ios文件存放路径、文件目录、缓存文件夹
- 关于 CentOS 7 里面 普通用户 Ulimit max user processes 值的问题
- java开发之——遍历文件夹的两种方式
- 使用google的zxing生成二维码
- oracle 使用sql查找出两张表相同的字段
- Zend Studio使用教程:将Zend Studio作为插件安装
- 底部悬浮通栏可以关闭广告位的实现方法
- 模板匹配——在一幅图像中匹配与模板相似的单个或者多个目标
- 冲刺第二阶段工作总结05
- Linux SVN 服务器配置以及客户端使用
- Quartz+Spring应用与集群
- NPAPI和PPAPI开发
- XML 默认命名空间
- 动态热修复技术的实现
- LaTeX技巧008:并排插入图片以及去掉图片编号
- 12.JVM的GC日志Full GC每个字段解释
- strace 分析 跟踪 进程错误
- CentOS tengine mysql 5.7 php 5.6