configparser模块
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模块的更多相关文章
- 用ConfigParser模块读写配置文件——Python
对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- Python学习笔记——基础篇【第六周】——PyYAML & configparser模块
PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之Co ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- 小白的Python之路 day5 configparser模块的特点和用法
configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...
- configparser模块的常见用法
configparser模块用于生成与windows.ini文件类似格式的配置文件,可以包含一节或多节(section),每个节可以有一个或多个参数(键=值) 在学习这个模块之前,先来看一个经常见到的 ...
- day20 hashlib、hmac、subprocess、configparser模块
hashlib模块:加密 import hashlib# 基本使用cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))print(cipher.h ...
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python封装configparser模块获取conf.ini值
configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
随机推荐
- sql 首写字母查询姓名(字段)
来自网上大神,不知道是谁,挂不上链接 /////////////////////// 1.生成方法函数 create function f_GetPy(@str nvarchar(4000)) ret ...
- 分页ajax+springmvc的简单实现
页面部分源码: <li class="paginItem"><a href="javascript:getNewsList(2);">2 ...
- ES5/标准 ECMAScript 内置对象
https://www.w3.org/html/ig/zh/wiki/ES5/%E6%A0%87%E5%87%86_ECMAScript_%E5%86%85%E7%BD%AE%E5%AF%B9%E8% ...
- maven中央仓库访问速度太慢的解决办法
方法一:修改settings.xml eclipse中集成的maven的settings.xml文件,找了半年也没找到,我们放弃eclipse中的maven,下一个最新的maven,并在eclipse ...
- MFC vs2012 Office2013 读写excel文件
近期在忙一个小项目(和同学一起搞的),在这里客户要求不但读写txt,而且可以读写excel文件,这里本以为很简单,结果...废话少说,过程如下: 笔者环境:win7 64+VS2012+Office2 ...
- hdu3530 单调队列
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- android MVC && MVP && MVVM分析和对比
相关:http://www.cnblogs.com/wytiger/p/5305087.html 出处http://blog.csdn.net/self_study,对技术感兴趣的同鞋加群544645 ...
- Java细粒度锁实现的3种方式
最近在工作上碰见了一些高并发的场景需要加锁来保证业务逻辑的正确性,并且要求加锁后性能不能受到太大的影响.初步的想法是通过数据的时间戳,id等关键字来加锁,从而保证不同类型数据处理的并发性.而java自 ...
- WordPress数据库优化技巧
各位站长都知道wordpress用久了就会越来越慢.今天就给大家介绍如何给自己的wordpress提速,分两种方法:1.插件属性wordpress的都知道其插件是相当的多,只要你能想得到的基本都有,在 ...
- [NHibernate]增删改操作
目录 写在前面 文档与系列文章 添加数据 删除数据 修改数据 添加修改数据 总结 写在前面 上篇文章介绍了nhibernate的基于面向对象的条件查询.对一个项目来说,增删改查是必不可少的,虽然实现方 ...