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 ...
随机推荐
- 624. Maximum Distance in Arrays
Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...
- jvm参数设置 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 这里有几个问题: 1. 各个参数的含义什么? 2. 为什么有的机器我将- ...
- sequence(bzoj 1367)
Description Input Output 一个整数R Sample Input 794820141518 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15 ...
- 前端的指导方针---css篇
英语是渣渣,想学英语,又不想花钱报培训班.看不懂的文章,还是翻译一下留着自己看吧. 引自 : https://github.com/bendc/frontend-guidelines HTML ...
- Notification通知创建
Notification通知创建 由于通知是一个远程视图,所以创建通知在状态栏显示需要用到三个主要的对象: 一.PendingIntent对象,用来承载Intent对象的,Intent对象主要是定义通 ...
- bit manipulation
WIKI Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorte ...
- HDU 5883 欧拉路径异或值最大 水题
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 动态规划:Ignatius and the Princess IV
#include<stdio.h> #include<string.h> #include<math.h> int main() { _int64 n,a; whi ...
- 从 modCount 看 java集合 fail-fast 机制
一.背景 在常见的Java的非线程安全集合类中(如HashMap.ArrayList),经常可以在一些修改结构的操作(如Add)中看到实例变量 modCount++ ,来统计集合的修改次数. 从注释也 ...
- java基础编程题
1. 某公司每月标准上班时间是160小时,每小时工资是30元. 如果上班时间超出了160小时,超出部分每小时按1.5倍工资发放.请编写程序计算员工月工资. package com.num2.lianx ...