Python中配置文件解析模块-ConfigParser

Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置)。
配置文件的格式是: []包含的叫section, section 下有option=value这样的键值
该模块的常用方法
1、config=ConfigParser.ConfigParser()
创建ConfigParser实例
2、config.sections()
返回配置文件中节序列
3、config.options(section)
返回某个项目中的所有键的序列
4、config.get(section,option)
返回section节中,option的键值
5、config.add_section(str)
添加一个配置文件节点(str)
6、config.set(section,option,val)
设置section节点中,键名为option的值(val)
7、config.read(filename)
读取配置文件
8、config.write(obj_file)
写入配置文件

例如:
配置文件 test.conf
[section1]
name = tank
age = 28
[section2]
ip = 192.168.1.1
port = 8080
代码示例:
# -* - coding: UTF-8 -* -
import ConfigParser
conf = ConfigParser.ConfigParser()------创建一个对象
conf.read("c:\\test.conf")
# 获取指定的section, 指定的option的值
name = conf.get("section1", "name")
print(name)
age = conf.get("section1", "age")
print age
#获取所有的section-----以数组形式输出
sections = conf.sections()
print sections
#写配置文件
# 更新指定section, option的值
conf.set("section2", "port", "8081")
# 写入指定section, 增加新option的值
conf.set("section2", "IEPort", "80")
# 添加新的 section
conf.add_section("new_section")
conf.set("new_section", "new_option", "http://www.cnblogs.com/tankxiao")
# 写回配置文件
conf.write(open("c:\\test.conf","w"))-------记住对配置文件的修改,最后一定要通过write方法写入到配置文件中,否则无效

  

原文地址:https://www.cnblogs.com/potato-chip/p/9310128.html

时间: 2024-08-07 15:43:55

Python中配置文件解析模块-ConfigParser的相关文章

Python中的logging模块【转】

基本用法 下面的代码展示了logging最基本的用法. 1 # -*- coding: utf-8 -*- 2 3 import logging 4 import sys 5 6 # 获取logger实例,如果参数为空则返回root logger 7 logger = logging.getLogger("AppName") 8 9 # 指定logger输出格式 10 formatter = logging.Formatter('%(asctime)s %(levelname)-8s:

Python中的logging模块

http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stackoverflow上查询到的一些内容. 官方文档 技术博客 基本用法 下面的代码展示了logging最基本的用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Python入门之Python中的logging模块

基本用法 下面的代码展示了logging最基本的用法. import logging import sys # 获取logger实例,如果参数为空则返回root logger logger = logging.getLogger("AppName") # 指定logger输出格式 formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s') # 文件日志 file_handler = logging.

Python中的random模块,来自于Capricorn的实验室

Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniform random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成的随机数n: a <= n <= b.如果 a <

python中查看可用模块

1.这种方式的问题是,只列出当前import进上下文的模块. 进入python命令行.输入以下代码: >>>import sys >>>sys.modules 2.在python命令行下输入: >>>help() help>modulespython中查看可用模块,布布扣,bubuko.com

python中动态导入模块

如果导入的模块不存在,Python解释器会报 ImportError 错误: >>> import something Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named something 有的时候,两个不同的模块提供了相同的功能,比如 StringIO 和 cStringIO 都提供了Strin

Python中的random模块

Python中的random模块 (转载自http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html) Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniform random.uniform的函数原型为:random.uniform(a, b),

WCF中配置文件解析

WCF中配置文件解析 2014-06-14 参考 WCF中配置文件解析 返回 在WCF Service Configuration Editor的使用中,我们通过配置工具自动生成了WCF服务端的config文件.现在我们来看下这个配置文件各个标签的意义: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <!-- 这个程序集我们在项目中有引用的 --> 4

解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题

问题: 我在ubuntu14.04下用python中的matplotlib模块内的pyplot输出图片不能显示中文,怎么解决呢? 解决: 1.指定默认编码为UTF-8: 在python代码开头加入如下代码 import sys reload(sys) sys.setdefaultencoding('utf-8') 2.确认你ubuntu系统环境下拥有的中文字体文件: 在终端运行命令"fc-list :lang=zh",得到自己系统的中文字体 命令输出如下: /usr/share/fon