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只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
随机推荐
- python-面向对象进阶
小总结: 面向对象的好处 更容易扩展,提高代码的使用效率,使代码组织性更强.更清晰,更适合复杂项目的开发 封装:把功能的实现细节封装起来,之暴露调用接口 多态:接口的继承 定义: 类 : 模板 ...
- php 时间加减
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 实现缓存预热
因为大型应用系统可能有几十个子系统,为了减轻数据库频繁读写压力.提高系统的运行速度.反映速度,大型应用系统都需要采用缓存机制提高运行效率.Redis 缓存预热实现将来大家很多基础数据都可以缓存获取,不 ...
- moosefs的安装使用及遇到的问题
一.获取源码安装包 到官网下载最新版本moosefs: https://moosefs.com/download/sources-archive-3-0.html到官网下载最新版本fuse源码 ht ...
- jquery更改输入框type为密码框password
很蛋疼的一个问题: <input type="text" id="e1" value="123" /> 用juqery将文本框变 ...
- 奇虎360诉腾讯QQ垄断案之我见(3Q大战之我见)
这两款软件我都在用,要说时间最长感情最深的应该是腾讯QQ,1999年诞生的那年就在用QQ了! 不过感情归感情,个人看法归个人看法,不能用感情来判断. 正所谓外行看热闹,内行看门道.从事实上讲在使用这两 ...
- 高性能集群软件Keepalived(1)
1介绍 Keepalived是linux下一个轻量级的高可用解决方案,与HeartBeat,RoseHA实现的功能类似,但是还是有差别.HeartBeat是一个专业的功能完善的高可用软件,它提供了HA ...
- [fiddler] 手机抓包
最近工作涉及到与原生app联调,需要抓取手机上的请求.借此机会研究了下fiddler,简直神器. 以下简单介绍通过fiddler进行手机抓包的方法,之后也会陆续更新fiddler的其他功能 设置fid ...
- 怎么样修改小猪cms(从功能库添加)模块关键字
需求:修改或者添加从功能库添加中的关键字 这里以添加咨询投诉为列: 找到wwwroot\PigCms\Lib\Action\User目录下的LinkAction.class.php文件(手动找不到直接 ...
- opencv 比较直方图方式 进行人脸检测对比
完整opencv(emgucv)人脸.检测.采集.识别.匹配.对比 //成对几何直方图匹配 public static string MatchHist() ...