Python模块-configparse模块
configparse模块用来解析配置文件
配置文件
[DEFAULT]
port = 3306
socket = /tmp/mysql.sock [mysqldump]
max_allowed_packet = 16M [myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
读取解析配置文件
# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import configparser config = configparser.ConfigParser() config.read('config.ini') #读取配置文件,并赋给config print(config.sections()) #读取配置文件中sections的标题,但是没有读取默认的section
print(config.default_section) #读取默认的section print('myisamchk' in config) #判断section是否在配置文件里,返回布尔类型 print(list(config['myisamchk'].keys())) #读取指定section和默认section里的option print(config['myisamchk']['read_buffer']) #获取section里option的值 print('read_buffer' in config['myisamchk']) #判断option是否在section中 #获取指定section和默认section里的option和option的值
for k,v in config['myisamchk'].items():
print(k,v)
运行结果

删除添加修改等操作
# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import configparser config = configparser.ConfigParser()
config.read('config.ini') print(config.has_section('python')) #判断section是否在配置文件中
config.add_section('python') #添加section到配置文件中
config.set('python','abc','123c') #给section中的option设置值,如果没有将创建
config.remove_option('python','abc') #删除option
config.remove_section('python') #删除section config.write(open('config_1.ini', "w")) #要重新写入才能成功完成操作
Python模块-configparse模块的更多相关文章
- python模块: hashlib模块, configparse模块, logging模块,collections模块
一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...
- python中ConfigParse模块的用法
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 配置文件的格式是: [...]包含的叫section section 下有option=value这样的键值 ...
- 常用模块 - configparse模块
一.简介 configparser模块在Python中是用来读取配置文件的,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节点(section),每个节可以有多个参数(键=值 ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- Python进阶-XVV hashlib模块、configparse模块、logging模块
1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...
- Python学习---重点模块之configparse
configparse模块常用于生成和修改常见的配置文档 生成配置模块:用字典写 import configparser config = configparser.ConfigParser() co ...
- Python学习日记(二十八) hashlib模块、configparse模块、logging模块
hashlib模块 主要提供字符加密算法功能,如md5.sha1.sha224.sha512.sha384等,这里的加密算法称为摘要算法.什么是摘要算法?它又称为哈希算法.散列算法,它通过一个函数把任 ...
- python学习-58 configparse模块
configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
随机推荐
- IDEA 配置Tomcat 跑Jeecg项目
最近搞了个国人开发的开源项目,还不错,记录一下踩过得坑; 首先项目开源地址 下载就可以; 准备工作作者以介绍,不再详述; 1:我使用的IDEA作为开发工具- 首先导入pom.xml,下载依赖包(此过程 ...
- easyui的 一些经验
1. 渲染网络表格时,行操作 <th field="sort_num" width="10" data-options="field:'id', ...
- 2014阿里实习生面试题——MySQL如何实现索引的
这是2014阿里实习生北京站二面的一道试题: 在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,比如MyISAM和InnoDB存储引擎. MyISAM索引实现: MyI ...
- HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
- html 文本输入框效果大汇集
html 文本输入框效果大汇集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- flex TweenLite
本贴已在 AS天地会转发,大家可以参考:http://bbs.actionscript3.cn/viewthread.php?tid=11090&pid=91142&page=1&am ...
- Unity 中 GetComponentsInChildren 的应用
在实际项目中,我们经常要去查找一个节点下的某个子节点,但是子节点太多,或者每次我们都要去自己写GameObject.FindChald("xxx")实在是太过繁琐,那么这是后就可以 ...
- dedecms常用标签
下面总结了58种常见的标签调用,包括关键描述调用.指定调用栏目.列表文章调用.频道栏目调用.当前栏目名称.栏目导航调用.模板路径调用.网站标题调用.友情链接调用.网站版权调用.网站备案调用.当前位置调 ...
- 剑指offer——扑克牌的顺子
思想: 1.先将输入的几个数进行排序,sort函数是#include<algorithm>下的. 2.统计0的个数,以及相邻数的差值,比较0的个数及差值的和.看是否可以用大王填充中间的差值 ...
- Docker 搭建本地Registry
Docker已经将Registry开源,Registry本身也是一个容器. 1. 修改配置/etc/docker/daemon.json,去掉docker默认的https的访问 里面的内容是一个j ...