python 解析 配置文件
资料: https://docs.python.org/3/library/configparser.html
环境
python 3.4.4
RawConfigParser方式
example.cfg文件:
[Section1]
an_int =
a_bool = true
a_float = 3.1415
baz = fun
bar = Python
foo = %(bar)s is %(baz)s!
test_example_ini.py 文件:
import configparser def test_write():
config = configparser.RawConfigParser()
config.add_section('Section1')
config.set('Section1', 'an_int', '')
config.set('Section1', 'a_bool', 'true')
config.set('Section1', 'a_float', '3.1415')
config.set('Section1', 'baz', 'fun')
config.set('Section1', 'bar', 'Python')
config.set('Section1', 'foo', '%(bar)s is %(baz)s!') with open('example.cfg', 'w',encoding="utf-8") as configfile:
config.write(configfile) def test_read():
config = configparser.RawConfigParser()
config.read('example.cfg') a_float = config.getfloat('Section1', 'a_float')
an_int = config.getint('Section1', 'an_int')
print (a_float + an_int) if config.getboolean('Section1', 'a_bool'):
print (config.get('Section1', 'foo')) if __name__=="__main__":
test_write()
test_read()
输出:
18.1415
%(bar)s is %(baz)s!
同时生成example.cfg文件,内容如下:
[Section1]
an_int = 15
a_bool = true
a_float = 3.1415
baz = fun
bar = Python
foo = %(bar)s is %(baz)s!
ConfigParser方式
mysql.cfg 文件内容:
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
skip-external-locking = True
old_passwords = 1
skip-bdb = True
skip-innodb = False [oracle]
user = root
password = hello
test_mysql_cfg.py 文件内容:
import configparser def test_write():
config = configparser.ConfigParser()
config.read('mysql.cfg') config.set("mysqld", "mysql_pass", "")
config.write(open('mysql.cfg', "w")) def test_read():
config = configparser.ConfigParser()
config.read('mysql.cfg') s = config.sections()
print ('section:', s) o = config.options("mysqld")
print ('mysqld:', o) v = config.items("mysqld")
print ('mysql:', v) mysql_user = config.get("mysqld", "user")
mysql_pid = config.get("mysqld", "pid-file")
mysql_old = config.getint("mysqld", "old_passwords")
mysql_skipbdb = config.get("mysqld", "skip-bdb") print (mysql_user, mysql_pid, mysql_old, mysql_skipbdb) if __name__=="__main__":
test_write()
test_read()
执行结果:
section: ['mysqld', 'oracle']
mysqld: ['user', 'pid-file', 'skip-external-locking', 'old_passwords', 'skip-bdb', 'skip-innodb', 'mysql_pass']
mysql: [('user', 'mysql'), ('pid-file', '/var/run/mysqld/mysqld.pid'), ('skip-external-locking', 'True'), ('old_passwords', ''), ('skip-bdb', 'True'), ('skip-i
nnodb', 'False'), ('mysql_pass', '123456')]
mysql /var/run/mysqld/mysqld.pid 1 True
同时mysql.cfg变化如下:
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
skip-external-locking = True
old_passwords = 1
skip-bdb = True
skip-innodb = False
mysql_pass = 123456 [oracle]
user = root
password = hello
python 解析 配置文件的更多相关文章
- Python解析配置文件模块:ConfigPhaser
算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...
- python模块之ConfigParser: 用python解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- Python 模块之 ConfigParser: 用 Python 解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...
- python 解析配置文件
settings.cfg [english] greeting = Hello [french] greeting = Bonjour [files] home = /usr/local bin = ...
- [Python]ConfigParser解析配置文件
近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...
- python解析模块(ConfigParser)使用方法
python解析模块(ConfigParser)使用方法 很多软件都有配置文件,今天介绍一下python ConfigParser模块解析配置文件的使用方法 测试配置文件test.conf内容如下: ...
- 使用Python解析JSON数据
使用Python解析百度API返回的JSON格式的数据 # coding:utf-8 # !/usr/bin/env python import matplotlib.pyplot as plt fr ...
- 使用Python解析JSON数据的基本方法
这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下: ----------------------------------- ...
- python解析robot framework的output.xml,并生成html
一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...
随机推荐
- 利用ASP.NET AJAX的Timer讓GridView每隔一段時間做到自動換頁的功能
最近在討論區看到這個問題,小弟利用asp.net ajax的timer來實作這個功能 利用timer每隔一段時間,讓gridview自動跳頁並且更新gridview的內容 asp.net(c#) Gr ...
- 怎么捕获和记录SQL Server中发生的死锁
我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方式有一个很大的敝端,就是消耗很大.据国外某大神测试,profiler甚至可以占到服 务器总带宽的35%,所以,在 ...
- sp_addlinkedserver的一些操作
sp_addlinkedserver 创建一个链接的服务器,使其允许对分布式的.针对 OLE DB 数据源的异类查询进行访问.在使用 sp_addlinkedserver 创建链接的服务器之后,此服务 ...
- windows下eclipse+hadoop2
windows下eclipse+hadoop2.4开发手册 1.解压下载的hadoop2.4,到任意盘符,例如D:\hadoop-2.4.0. 2.设置环境变量 ①新建系统变量,如下所示. ②将新建的 ...
- kvc简单实现
除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性 kvc: kvc key value coding 键值对编码 可以通过 ...
- iOS app闪退的一般原因
1.函数无限递归爆栈(表视图返回Cell和返回行高的方法互相调用)2.某对象无法解析某个方法(没做类型转换.或者代理没实现某个方法)3.访问了某个已经被释放的对象(ARC之后不太有)4.从Bundle ...
- Java设计模式(学习整理)---策略模式
1. 模式定义 把会变化的内容取出并封装起来,以便以后可以轻易地改动或扩充部分,而不影响不需要变化的其他部分: 2.模式本质: 少用继承,多用组合,简单地说就是:固定不变的信息 ...
- Linux中的find指令
find find是最常见和最强大的查找命令,在磁盘中查找文件,用它找到任何你想找的文件,就是速度有点慢. find path -option [ -print ] [ ...
- SGU 134.Centroid(图心)
SGU链接: 时间限制:0.25s 空间限制:4M 题意: 给出一个树(节点数<=16000),一个节点的重量定义为从树中去除这个点后,新得到的所有树中节点最多的树的节点数.树的中心定义为所有节 ...
- HDU 4010.Query on The Trees 解题报告
题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...