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 ...
随机推荐
- Go map例题
package main import "fmt" //map例题 //寻找最长不含有重复字符的子串 // abcabcbb -> abc //pwwkew ->wke ...
- CSU 1290 DP解决数学期望问题
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1290 题目大意: 给定k个数,每次可以生成0-N-1中的任何一个数,k个数中出现不同的整 ...
- Uva10294 Arif in Dhaka (置换问题)
扯回正题,此题需要知道的是置换群的概念,这点在刘汝佳的书中写的比较详细,此处不多做赘述.此处多说一句的是第二种手镯的情况.在下图中“左图顺时针转1个位置”和“右图顺时针旋转5个位置”是相同的,所以在最 ...
- selenide01---截图
1.junit:import com.codeborne.selenide.junit.ScreenShooter; @Rule public ScreenShooter makeScreenshot ...
- WebApi下载附件文件
WebApi下载附件文件 1. [RoutePrefix("down")] public class FilesController : ApiController { [GET( ...
- Postman调试依赖登录接口的3种方法
在接口测试种, 我们经常会遇到有些接口登录后才能访问.我们在使用Postman调试这种接口时一般有3种方法: 依次请求 如果有登录接口的文档,或者通过抓包比较容易抓出登录请求的参数和格式,可以先使用P ...
- 2016 Multi-University Training Contest 6 solutions BY UESTC
A Boring Question \[\sum_{0\leq k_{1},k_{2},\cdots k_{m}\leq n}\prod_{1\leq j< m}\binom{k_{j+1}}{ ...
- Flex里监听mouseDownOutside事件解决弹出窗口点击空白关闭功能
其实当用户在使用 PopUpManager 打开的某个组件外部单击时,会从该组件分派一个mouseDownOutside事件 监听该事件就能实现点击空白处关闭窗口的功能 this.addEventLi ...
- 重载OverLoad。隐藏new
<1> using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...
- 西门子PLC学习笔记六-(Step7指令简单介绍)
1.指令操作数 指令操作数由操作标示符和參数组成. 操作标识符由主标识符和辅标识符组成. 主标识符有:I(输入过程影像寄存器).Q(输出过程映像寄存器).M(位寄存器).PI(外部输入寄存器).PQ( ...