configparser模块

echo   $@ $# $? $*

具体代码示例代码

import ConfigParser
import os class Config(object):
def __init__(self, config_filename="cgss.conf"):
print(config_filename)
file_path = file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), config_filename) #注意这句的路径问题
self.cf = ConfigParser.ConfigParser()
self.cf.read(file_path)
print(self.get_sections()) def get_sections(self):
return self.cf.sections() def get_options(self, section):
return self.cf.options(section) def get_content(self, section):
result = {}
for option in self.get_options(section):
value = self.cf.get(section, option)
result[option] = int(value) if value.isdigit() else value
return result ret = Config().get_content("mongo")
print ret

cgss.cnf   
[notdbMysql]
host = 192.168.1.101
port = 3306
user = root
password = python123

详解

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','')
config.write(open("xxoo.txt",'w'))

configparser模块的更多相关文章

  1. 用ConfigParser模块读写配置文件——Python

    对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...

  2. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  3. Python学习笔记——基础篇【第六周】——PyYAML & configparser模块

    PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之Co ...

  4. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  5. 小白的Python之路 day5 configparser模块的特点和用法

    configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...

  6. configparser模块的常见用法

    configparser模块用于生成与windows.ini文件类似格式的配置文件,可以包含一节或多节(section),每个节可以有一个或多个参数(键=值) 在学习这个模块之前,先来看一个经常见到的 ...

  7. day20 hashlib、hmac、subprocess、configparser模块

    hashlib模块:加密 import hashlib# 基本使用cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))print(cipher.h ...

  8. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  9. python封装configparser模块获取conf.ini值

    configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...

随机推荐

  1. SQL Server日志文件(LDF文件)

    一.日志文件过大处理方法: 1.设置数据库模式为简单模式:ALTER DATABASE 数据库名 SET RECOVERY SIMPLE 或者选中数据库-属性-选项-恢复模式设置为简单. 2.收缩日志 ...

  2. mybatis 使用经验小结

    一.多数据源问题 主要思路是把dataSource.sqlSesstionFactory.MapperScannerConfigurer在配置中区分开,各Mapper对应的包名.类名区分开 <? ...

  3. margin-top无效的问题解决方法

    今天碰到了margin-top无效的问题,解决方法也有很多一行代码就解决了 解决办法: 1.设置父元素或者自身的display:inline-block;(IE6.IE7不识别inline-block ...

  4. RPM命令用法

    安装一个包 rpm –ivh 升级一个包 rpm -Uvh 移走一个包 rpm -e 4.校验rpm包 rpm -V < rpm package name> 5.查询一个包是否被安装 rp ...

  5. npm+node+cordova+ionic 版本匹配

    npm 2.15.8 node 4.4.7 cordova 6.1.0 ionic 1.7.16

  6. 招聘前端、Java后端开发、测试、Mysql DBA

    公司介绍: http://www.lagou.com/gongsi/43095.html http://www.yamichu.com 简历发到: zhuye@yamichu.com 招聘职位: JA ...

  7. 基于OpenSSL实现C/S架构中的https会话

    在实际生产中实现公司内部的web服务器加密访问时,我们就需要实现公司内部的私钥CA,并且完成对web服务器的签署请求,这样我们就可以在自身的内部机构实现对数据的机密性.完整性.身份验证的访问与传输 实 ...

  8. C#嵌入dll到资源释放的问题

    有些程序运行的时候,可能调用外部的dll,用户使用时可能会不小心丢失这些dll,导致程序无法正常运行,因此可以考虑将这些dll嵌入到资源中,启动时自动释放.对于托管的dll,我们可以用打包软件合成一个 ...

  9. 篇一:js中动态加载---append

    之前是一行代码,不能动态加载,新的需求要动态加载,使用append $('#Order_information').append(' <div class="single_produc ...

  10. CentOS 7.2 安装配置mysql主从服务器

    MySQL官方压缩包安装: 1:下载mysql官方版本,此处以目前最新版本5.7.14为例,下载的64位版本文件为: mysql-5.7.14-linux-glibc2.5-x86_64.tar 2: ...