Python模块之: ConfigParser 配置文件读取

 

ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型。

ConfigParser使用用的配置文件格式由一个或多个命名的节(section)组成,每一节包含由key和value构成的选项(option)。

在一节中每行列出一个选项。行以选项名开头,选项名与值之间用一个冒号(:)或一个等号(=)分开。

1.读取配置文件

-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型
 
2.写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
需要调用write将内容写入配置文件。
 
示例
test.conf
[sec_a]
a_key1 = 20
a_key2 = 10
 
[sec_b]
b_key1 = 121
b_key2 = b_value2
b_key3 = $r
b_key4 = 127.0.0.1
 
getConfig.py
import ConfigParser

cf = ConfigParser.ConfigParser()

#allow_no_value=True参数可以允许配置文件的选项中只有key而没有value #read config cf.read("test.conf")

#read可以设置读取多个配置文件,使用列表形式即可 # return all section secs = cf.sections() print 'sections:', secs opts = cf.options("sec_a") print 'options:', opts kvs = cf.items("sec_a") print 'sec_a:', kvs #read by type str_val = cf.get("sec_a", "a_key1") int_val = cf.getint("sec_a", "a_key2") print "value for sec_a's a_key1:", str_val print "value for sec_a's a_key2:", int_val #write config #update value cf.set("sec_b", "b_key3", "new-$r") #set a new value cf.set("sec_b", "b_newkey", "new-value") #create a new section cf.add_section('a_new_section') cf.set('a_new_section', 'new_key', 'new_value') #write back to configure file cf.write(open("test.conf", "w"))

Python模块之: ConfigParser 配置文件读取的更多相关文章

  1. Python模块学习 - ConfigParser

    配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...

  2. python接口自动化测试 - configparser配置文件解析器详细使用

    configparser简介 ConfigParser模块已在Python 3中重命名为configparser 该模块定义了ConfigParser类. ConfigParser类实现一种基本的配置 ...

  3. python 模块之-configparser

    python 模块configparser   配置文件模块 import configparser    config = configparser.ConfigParser() config[&q ...

  4. Python模块之ConfigParser - 读写配置文件

    Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...

  5. python模块之ConfigParser: 用python解析配置文件

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...

  6. Python 模块之 ConfigParser: 用 Python 解析配置文件

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...

  7. Python 模块续 configparser、shutil、XML、paramiko、系统命令、

    一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...

  8. Python模块:configparser、hashlib、(subprocess)

    configparser模块: 此模块用于生成和修改常见配置文档. 一个常见配置文件(.ini的后缀名)格式如下: [DEFAULT] # DEFAULT 是指后面的字典里都会默认有的内容 Serve ...

  9. python模块之configparser模块

    configparser模块:用于按一定格式创建配置文件 创建 import configparser config = configparser.ConfigParser() config['DEF ...

随机推荐

  1. maven创建web工程Spring配置文件找不到

    使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...

  2. 实现HTML格式的数据报表邮件

  3. Delphi 的 Utf-8 转换

    新版的 Delphi 應該不用這麼麻煩, 據說只要直接在 AnsiString, WideString, UTF8String 之間 assign 時就會自動幫你做轉換 (沒用過, 不知道是不是真的這 ...

  4. python快速学习-常用內建模块

  5. js 实现自动调出键盘

    在app中,在页面加载完成之后,给输入框添加一个focus,不能自动调出软键盘,可以用以下方式实现: //触发键盘 $("#content").on("touchstar ...

  6. 2017-02-23 错误信息:未在本地计算机上注册“Microsoft.ACE.oledb.12.0”提供程序。

    问题如题,解决办法:去 http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/Access ...

  7. wpf数据绑定的论述

    (1)绑定模式: <Lable x:Name=lab Content={binding UserName} /> <!--binding相当于SetBinding--> Con ...

  8. Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions

    Model compatibility cannot be checked because the database does not contain model metadata. Ensure t ...

  9. Spring mvc服务端消息推送(SSE技术)

    SSE技术是基于单工通信模式,只是单纯的客户端向服务端发送请求,服务端不会主动发送给客户端.服务端采取的策略是抓住这个请求不放,等数据更新的时候才返回给客户端,当客户端接收到消息后,再向服务端发送请求 ...

  10. 手动安装mysql-5.0.45.tar.gz

    Linux下编译安装 安装环境:VMware9(桥接模式) + Linux bogon 2.6.32-642.3.1.el6.x86_64(查看linux版本信息:uname -a) 先给出MySQL ...