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#1133 二分·二分查找之k小数
原题地址 经典问题了,O(n)时间内找第k大的数 代码: #include <iostream> using namespace std; int N, K; int *a; int se ...
- 【字符串+BFS】Problem 7. James Bond
https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个 ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- No route info of this topic
使用rocketmq时报错 com.alibaba.rocketmq.client.exception.MQClientException: No route info of this topic, ...
- 【frameset】frameset设置不能拖动
<frameset rows='20%,*' > <!-- row 行 col 列 分行列要为rows cols --> <frame s ...
- 为什么zookeeper的节点配置的个数必须是奇数个?
zookeeper有这样一个特性:集群中只要有过半的机器是正常工作的,那么整个集群对外就是可用的.也就是说如果有2个zookeeper,那么只要有1个死了zookeeper就不能用了,因为1没有过半, ...
- 2017多校Round4(hdu6067~hdu6079)
补题进度:10/13 1001 待填坑 1002(kmp+递推) 题意: 有长度为n(<=50000)的字符串S和长度为m(m<=100)的字符串T,有k(k<=50000)组询问, ...
- Android拍照、摄像方向旋转的问题 代码具体解释
近期做了个拍照.摄像的应用.遇到了拍照.摄像的图像相对于现实.翻转了90度.原因:相机这个硬件的角度是横屏的角度,所以会出现都是横屏的. 1.照相.摄影预览图像的正确角度显 示: public sta ...
- EJB学习(三)——java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to..
在上一篇博客介绍了怎样使用使用Eclipse+JBOSS创建第一个EJB项目,在这期间就遇到一个错误: Exception in thread "main" java.lang.C ...
- The sandbox is not sync with the Podfile.lock
github下载的Demo,很多时候使用到CocoaPods,有的时候因为依赖关系或者版本问题不能编译运行. 出现 以下错误 The sandbox is not sync with the Podf ...