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只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
随机推荐
- title与alt的区别
html中的title属性和alt属性让人有些混淆. 以前不知道有title这个属性,第一次用到它时,就和alt产生了混淆.一位朋友告诉我说,alt是图片img标签里用的,title是超链接里用的, ...
- Windows phone应用开发[19]-RSA数据加密
在这个系列的第十六章节中Windows phone应用开发[16]-数据加密 中曾详细讲解过windows phone 常用的MD5,HMAC_MD5,DES,TripleDES[3DES] 数据加密 ...
- 网易蜂巢微服务架构:用RabbitMQ实现轻量级通信
本次分享内容由三个部分组成: 微服务架构与MQ RabbitMQ场景分析与优化 RabbitMQ在网易蜂巢中的应用和案例分享 1微服务架构与MQ 微服务架构是一种架构模式,它将单体应用划分成一组微小的 ...
- python 列表生成式
r = [x*x for x in range(10)] r list类型
- transformClassesWithDexForDebug
转自:http://blog.sina.com.cn/s/blog_6f3828770102w30b.html
- 结构体里的“位域”(bit-field)结构
首先看一个题目: #include <iostream> using namespace std; #include <string.h> typedef struct AA{ ...
- prototype 原型
在我刚学习javascript的时候对于原型的概念很模糊,现在也不是很懂,希望下面的文章对有相同问题的朋友有帮助,如果有误希望指正: prototype用于通常用于构造函数中,公用方法的继承上.构造函 ...
- zabbix告警使用sendEmail
1sendmail介绍 详细介绍见官网:http://caspian.dotconf.net/menu/Software/SendEmail/ 2使用sendEmail sendEmail是个十分优秀 ...
- awk 用法(使用入门)
转自:http://www.cnblogs.com/emanlee/p/3327576.html awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...