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 ...
随机推荐
- &与&
- exp/imp 有很多弊端
弊端1. 空表 无法执行导出操作弊端2. 高版本的导出文件 无法使用 低版本的 oracle软件 导入 环境准备:create table test0707(n1 date); 认证弊端1 案例1. ...
- printf 格式化最常用用法
printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...
- websocket以及自定义协议编程一些总结
以下仅供自己翻阅,因为时间久了会忘2.发送缓冲区主要是为了处理发送前一些小内容,可以自己控制flush,或者write的不是那么频繁因为没必要.至于大内容就没必要了.3.其实tcp以上的通信协议也好, ...
- select控件变成可输入状态
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery判断文本框是否为空
1.引用jQuery库 <script src="/static/js/jquery_v1.6.1.js" type="text/javascript"& ...
- js控制 点一下增加一个输入框,点一下增加一个输入框……
<div> <div> 附件1:<input type="file" id="file1" name="file1&qu ...
- php实现的太平洋时间和北京时间互转的自定义函数
date_default_timezone_set('Asia/Shanghai'); $time = time(); } date_default_timezone_set('Pacific/Api ...
- Shell 控制并发
方法1: #!/bin/bash c=0 for i in `seq -w 18 31`;do while [ $c -ge 3 ];do c=$(jobs -p |wc -w) sleep 1s d ...
- TC2.0中怎样调用汇编程序
转载于: TC2.0中怎样调用汇编程序 一.概述 TC是美国BORLAND 公司在IBM PC机上开发的一个高效.优化的C编译程序,它自带高效的全屏幕编辑程序,在集成开发环境下可支持编辑.编译.连接调 ...