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 ...
随机推荐
- OC - 29.自定义布局实现瀑布流
概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...
- Linux中的find指令
find find是最常见和最强大的查找命令,在磁盘中查找文件,用它找到任何你想找的文件,就是速度有点慢. find path -option [ -print ] [ ...
- modifytime是一个神奇的column name----这边文章是错的totally,因为我的实验不彻底。timestamp属性很神奇,头一个timestamp,会自动的成DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
在mysql里边modifytime是一个神奇的column name,试一下. 请执行sql语句 CREATE TABLE `test_time` ( `modifytime` timestamp ...
- javascript——迭代方法
<script type="text/javascript"> //五个迭代方法 都接受两个参数:要在每一项上运行的函数 和 运行该函数的作用域(可选) //every ...
- seaJs初体验
目录结构 模块定义define define(function(require,exports,module){ //exports可以把方法或属性暴露给外部 exports.name = 'hell ...
- javascript中的基本数据类型
在javascipt中有五大基本数据类型,列表如下: 1.数字 他们又又包括(正负整数,浮点数)十进制数,十六进制数与八进制数,指数和特殊数值NaN,(Infinity,-Infinity)正负无穷 ...
- 验证视图状态MAC失败解决方案
验证视图状态 mac 失败.如果此应用程序由网络场或群集承载 请确保 machinekey 配置指定了相同的 validationkey 和验证算法.不能在群集中使用 autogenerate. 总是 ...
- 用VS2005写一个 C 的类库和用 C#来调用的示例
一.用VS2005写一个 C 的类库的步骤: (1).建立一个空的Visual C++项目 (2).这时候在项目中可以看见 三个空目录 选中 "源文件" 目录,然后点鼠标右键,在弹 ...
- Asp.net 实现图片缩放 无水印(方法二)
public static System.Drawing.Image GetImage(string path) { try { if (path.StartsWith("http" ...
- js获取url中的参数对象、js生成带参数的url
// 获取url中的参数,并返回一个对象 $.getRequestData = function() { var url = location.search; //获取url中"?" ...