python - ConfigParser
ConfigParse 作用是解析配置文件。
配置文件格式如下
[test1]
num1: 10
[test2]
num1: 35
配置文件两个概念section和option, 在这个例子中第一个section是
[test1]
num1: 10
其中test1是section name, 而num1是option的name。ConfigParser同样是根据section和option来解析配置文件。
使用ConfigParser
import ConfigParser
conf = ConfigParser.ConfigParser()
解析对应的配置文件:
提供了两个方法
-read()
-readfp()
这两个函数都是读取配置文件内容,不同之处是read参数是文件,而readfp的参数是handle。
conf.read("test.config") conf.readfp(open("test.config"))
获取配置文件内容
-get(section,option)
-getinit(section, option)
-getfloat(section, option)
-getboolean(section, option)
这应该是最经常用到的函数,从配置文件中获取对应数值。get获取对应数值,后面三个函数在get的基础上进行了数值的转换。
conf.get('test1', 'num1')
conf.getint('test2', 'num1')
第一行返回的是字符串10,第二行返回的是数值35。其他两个函数同理。
需要注意的是getboolean(section,option)虽然返回True/False,在配置文件中可以为on/off True/False yes/no。方便了开关的配置。
其余的函数则是对section,option和item的显示和判断操作
比如has_section, 判断在配置文件中是否包含这个section
conf.has_section('test1')
类似函数has_option(section, option), 判断section中是否包含option
显示内容函数例
例如函数sections,显示所有的section
options, 显示一个section下所有的option
items,以(option,value)的形式显示section下所以的数值,返回列表。
>>> conf.items('test1')
[('num1', '10')]
>>> conf.sections()
['test1', 'test2']
>>> conf.options('test2')
['num1']
>>>
在ConfigParser中还有一类函数修改添加option,section然后写入到配置文件文档。
在我看来这是一个使用频率很低的函数。在一般程序中很少使用到。所以这个地方就不做讲解。
python - ConfigParser的更多相关文章
- python configparser模块
来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...
- python ConfigParser配置读写
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...
- python ConfigParser、shutil、subprocess、ElementTree模块简解
ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...
- [Python]ConfigParser解析配置文件
近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...
- Python configparser 读取指定节点内容失败
# !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 config = configparser ...
- python configparser使用
.ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成. 以 example.ini为例: [DEFAULT] ServerAliveInterval = 45 Co ...
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- Python ConfigParser的使用
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- 【转载】Python ConfigParser的使用
1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...
随机推荐
- 03 Javascript初识
Javascript语言(★★★★★) Javascript是基于对象和事件驱动的脚本语言,作用在客户端. 特点: * 交互性 * 安全性(不能访问本地的硬盘) ...
- appium运行报错.<init>(Lorg/openqa/selenium/remote/ErrorCodes;Z)V
最近这几天就在学习appium,搭建环境就耗费了很多时间,不得不承认自己够笨的了,然后我把环境搭建好,写完脚本的时候,就报这个错了,当时是从某个群里直接下载的demo,不得不吐槽说,够坑的,是能跑通, ...
- 我的c++学习(7)引用和复制构造函数
一.引用 什么是引用? 引用又称别名(alias),是一种非常特殊的数据类型.它不是定义一个新的变量,而是给一个已经定义的变量重新起一个别名,也就是 C++系统不为引用类型变量分配内存空间.引用主要用 ...
- HDU5727 Necklace(枚举 + 二分图最大匹配)
题目大概说有n个yang珠子n个yin珠子,要交替串成一个环形项链,有些yang珠子和某个yin珠子相邻这个yang珠子会不高兴,问最少有几个yang珠子不高兴. 自然会想到直接用状压DP去解,转移很 ...
- BZOJ1946 : [Ceoi2006]ANTENNA
首先通过随机增量法求出最小覆盖圆,作为答案的上界. 然后二分答案,检验的时候枚举每个点作为原点,求出其他每个点被包括在圆内的角度区间,然后扫描线即可. 时间复杂度$O(Tn^2\log n)$. #i ...
- nodejs express template (模版)的使用 (ejs + express)
var app=require("express").createServer(); app.set("view engine","ejs" ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- CentOS6.4 安装MongoDB
1.下载MongoDB(64位) http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz 或 http://pan.baidu.c ...
- [WP8.1UI控件编程]Windows Phone理解和运用ItemTemplate、ContentTemplate和DataTemplate
2.2.5 ItemTemplate.ContentTemplate和DataTemplate 在理解ItemTemplate.ContentTemplate和DataTemplate的关系的之前,我 ...
- Codeforces Beta Round #5
A题,无聊的题目. #include <cstdio> #include <string> #include <cstring> #include <cmat ...