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 ...
随机推荐
- 【置换】G. Poker 2.0
https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/G [题意] 题意很简单,就是“鸽尾式”洗扑克,问洗m次各张牌的位置 [思路] 牌 ...
- php的抽象类
php的抽象类 //定义一个老虎类 abstract class Tiger{ public abstract function climb(); } //定义一个孟加拉虎类 class MTiger ...
- bit manipulation
WIKI Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorte ...
- poj1330+hdu2586 LCA离线算法
整整花了一天学习了LCA,tarjan的离线算法,就切了2个题. 第一题,给一棵树,一次查询,求LCA.2DFS+并查集,利用深度优先的特点,回溯的时候U和U的子孙的LCA是U,U和U的兄弟结点的子孙 ...
- Codeforces 653D Delivery Bears【二分+网络流】
题目链接: http://codeforces.com/problemset/problem/653/D 题意: x个熊拿着相同重量的物品,从1号结点沿着路走到N号结点,结点之间有边相连,保证可以从1 ...
- vue学习001 --环境搭建
系统 : win cmd: cmder 链接:https://cmder.net/ 1.安装node.js 链接地址: http://cdn.npm.taobao.org/dist/node/v10. ...
- Maven新建webapp项目报错Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE
Windows-Preferences 在搜索框输入maven,点击下面的Archetypes--->Add Remote Catalog... 对应输入 http://repo1.maven. ...
- 阿里oss上传图片react组件alioss-react,vue组件alioss-vue (不用我先收藏着,后端看下前端处理方法)
1.介绍 最近开发了一个项目,其中需要一个上传图片到阿里云的 oss 上面,就是上传图片到阿里云的 oss 上面. 因为之前开发过 vue 的阿里云 oss 上传,所以直接复制粘 vue 的组件. 因 ...
- 使用Spring定时任务并且通过AOP监控任务执行情况
原文:http://www.open-open.com/code/view/1426250803279 本文讲的是通过Spring注解的方式实现任务调度.只要引入了spring-context包就能够 ...
- Meteor Blaze
Blaze是Meteor 软件包用于构建现场反应模板. Render方法 这种方法被用于绘制模板到DOM.首先,我们将创建 myNewTemplate 之后渲染. 我们增加 myContainer 这 ...