资料: 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 解析 配置文件的更多相关文章

  1. Python解析配置文件模块:ConfigPhaser

    算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...

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

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

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

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

  4. python 解析配置文件

    settings.cfg [english] greeting = Hello [french] greeting = Bonjour [files] home = /usr/local bin = ...

  5. [Python]ConfigParser解析配置文件

    近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...

  6. python解析模块(ConfigParser)使用方法

    python解析模块(ConfigParser)使用方法 很多软件都有配置文件,今天介绍一下python ConfigParser模块解析配置文件的使用方法 测试配置文件test.conf内容如下: ...

  7. 使用Python解析JSON数据

    使用Python解析百度API返回的JSON格式的数据 # coding:utf-8 # !/usr/bin/env python import matplotlib.pyplot as plt fr ...

  8. 使用Python解析JSON数据的基本方法

    这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下:     ----------------------------------- ...

  9. python解析robot framework的output.xml,并生成html

    一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...

随机推荐

  1. ASP.NET如何发布更新

    如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...

  2. MySQL性能状态查看方式

    1. QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...

  3. WCF上传、下载、删除文件

    关键代码: --上传的stream处理,转为bytep[] private void Parse(Stream stream, Encoding encoding) { this.Success = ...

  4. Linux下使用NMON监控、分析系统性能 -转载

    原帖地址:http://blog.itpub.net/23135684/viewspace-626439/ 谢谢原帖大人 一.下载nmon. 根据CPU的类型选择下载相应的版本:http://nmon ...

  5. IOS 中得runloop 详细解释

    1.Runloop基础知识- 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如触 ...

  6. IOS 生成设备唯一标识

    前言 iOS设备5.0以上放弃使用[[UIDevice currentDevice] uniqueIdentifier]来获得设备唯一ID iOS设备私有方法禁止用户获取和使用IMEI 需求 需要一个 ...

  7. Cocos2dx开发(2)——Win8.1下Cocod2dx 3.2环境搭建

    正式开始搭建cocos2dx环境,回到熟悉的VS 1.Python安装配置 这一步很简单,下载Python2.7.3,笔者直接用软件助手直接下载安装,最后配置环境变量 如下成功 2.cocos2dx ...

  8. linux 监控服务器脚本

    #!/bin/bash ctime=`date +%x%T`monitor_dir=/home/jk/if [ ! -d $monitor_dir ]; then    mkdir $monitor_ ...

  9. iOS播放短的音效

    在IOS中,有的时候需要播放很简短的声音文件,比如系统声音等,我们需要使用到下面的方式来播放声音: // 一.引入头文件 #import <AudioToolbox/AudioToolbox.h ...

  10. Yukari's Birthday

    hdu4430:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题意:题目的意思就是给你一个s,让你求k,r,其中k,r,满足:k^1+k^2+..... ...