python ConfigParser模块 配置文件解析
ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links= [mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost config]# cat 1c.py
import ConfigParser
parser=ConfigParser.SafeConfigParser()
parser.read('/etc/my.cnf')
print parser.get('mysqld','socket')
[root@localhost config]# python 1c.py
/var/lib/mysql/mysql.sock
[root@localhost config]# cat 2c.py
import ConfigParser
parser=ConfigParser.SafeConfigParser()
parser.read('/etc/my.cnf')
print parser.sections() #打印配置文件里面的节点
for nodename in parser.sections():
print "nodename:",nodename
print "optionsname:",parser.options(nodename) #获取节点名里面的选项
for name,value in parser.items(nodename): #以字典的方式返回
print "%s=%s"%(name,value) [root@localhost config]# python 2c.py
['mysqld_safe', 'mysqld']
nodename: mysqld_safe
optionsname: ['log-error', 'pid-file']
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
nodename: mysqld
optionsname: ['datadir', 'socket', 'symbolic-links', 'user']
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
user=mysql
[root@localhost config]# cat 3c.py
import ConfigParser
parser=ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read('/etc/my.cnf')
for node in parser.sections():
for optionname in parser.options(node):
print "%s=%s"%(optionname,parser.get(node,optionname)) #利用get方法返回一个值的整型,但是log-bin没有值,所以返回None
[root@localhost config]# python 3c.py
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
user=mysql
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=None
[root@localhost config]# cat 4c.py
import ConfigParser
parser=ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read('/etc/my.cnf')
for section in ['mysqld','mysqld1']:
print "[%s] is exists?:%s" %(section,parser.has_section(section))
print "[%s]serverid option is exists?:%s"%(section,parser.has_option(section,'serverid'))
[root@localhost config]# python 4c.py
[mysqld] is exists?:True
[mysqld]serverid option is exists?:True
[mysqld1] is exists?:False
[mysqld1]serverid option is exists?:Fals
[root@localhost config]# cat 5c.py
import ConfigParser
import sys
parser=ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read('/etc/my.cnf')
parser.add_section('mysqld1')
parser.set('mysqld1','serverid','')
parser.set('mysqld1','log-bin')
parser.write(sys.stdout)
[root@localhost config]# python 5c.py
[mysqld1]
serverid = 2
log-bin [mysqld_safe]
log-error = /var/log/mysqld.log [mysqld]
socket = /var/lib/mysql/mysql.sock
datadir = /var/lib/mysql
log-bin
serverid = 2
symbolic-links = 0
user = mysql
修改serverid属性值并写进my.cnf文件(节点顺序也反了)
[root@localhost config]# cat 5c1.py
import ConfigParser
parser=ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read('/etc/my.cnf')
parser.set('mysqld','serverid','')
with open('/etc/my.cnf','w') as f:
parser.write(f)
[root@localhost config]# python 5c1.py
[root@localhost config]# cat 5c1.py
import ConfigParser
parser=ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read('/etc/my.cnf')
parser.set('mysqld','serverid','')
with open('/etc/my.cnf','w') as f:
parser.write(f)
[root@localhost config]# cat /etc/my.cnf
[mysqld_safe]
log-error = /var/log/mysqld.log [mysqld]
socket = /var/lib/mysql/mysql.sock
datadir = /var/lib/mysql
log-bin
serverid = 8
symbolic-links = 0
user = mysql
python ConfigParser模块 配置文件解析的更多相关文章
- python之模块配置文件ConfigParser(在python3中变化较大)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ConfigParser(在python3中为configparser) #特别注意:py ...
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- python -ConfigParser模块讲解
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Python - configParser模块学习
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- 【python】python configparser模块
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...
- Python3-configparser模块-配置文件解析器
Python3中的configparser模块主要用于处理类似于windows ini 文件结构的配置文件 1.configparser模块提供实现基本配置语言的ConfigParser类 2.配置文 ...
- python ConfigParser 模块
ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...
随机推荐
- Hihocoder #1067 : 最近公共祖先·二
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中 ...
- HDU 5695 Gym Class
拓扑排序. #include<cstdio> #include <iostream> #include<cstring> #include<cmath> ...
- 1370 - Bi-shoe and Phi-shoe(LightOJ1370)(数论基础,欧拉函数)
http://lightoj.com/volume_showproblem.php?problem=1370 欧拉函数: 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. φ(n) ...
- 210 Course ScheduleII
/* * 210 Course ScheduleII * 2016-6-9 by Mingyang * http://www.jyuan92.com/blog/leetcode-course-sche ...
- Office WORD如何为每一页设置不同的页眉页脚
如下图所示,我想要为封面和目录,摘要等等设置不同的页眉页脚(一般封面和目录不需要页脚) 而从正文开始,套用相同的页眉和以页数作为页脚(注意"第一章 绪论"不是这个文档的第一页) ...
- Leetcode--easy系列10
#205 Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...
- poj 1258 Agri-Net(Prim)(基础)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44487 Accepted: 18173 Descri ...
- 【Android开发-4】进入实践,最喜欢折腾的计算器
前言:前面对项目文件有了感性认识.接下来我们就须要通过不断实践,对项目的文件有理性的认识. 曾经折腾Unity3d.IOS开发都是拿计算器开刀.所以这次Android开发实践也不例外,继续拿计算器折腾 ...
- 获取当前时间 YYYY-MM-DD
1.函数封装 /** * 获取当前时间 * 格式YYYY-MM-DD */ Vue.prototype.getNowFormatDate = function() { var date = new D ...
- Vs2017添加引用时报错 未能正确加载“ReferenceManagerPackage”包。
Vs2017添加引用时报错未能正确加载“ReferenceManagerPackage”包. 最近新装了2017,开始前几天还好, 可是最近在添加引用时,报错 -------------------- ...