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 ...
随机推荐
- HDU 4772 Zhuge Liang's Password (简单模拟题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 ...
- centos安装 Falcon+
1:环境 准备 : 安装 go环境 :下载 - Golang中国 参照 :http://www.cnblogs.com/Amos-Turing/p/8494250.html 安装 mysql 安装 r ...
- xpath中如何使用变量
xpath (python)xpath中如何使用变量描述: 在xpath中该如何使用变量,想选择id是某个值的元素,这个值是个变量. response.xpath('//div[@id=val]'). ...
- activiti--5 -----------------Activiti 工作流 流程各个步骤所涉及到的表
ACT_RE_*: 'RE'表示repository. 这个前缀的表包含了流程定义和流程静态资源 (图片,规则,等等). ACT_RU_*: 'RU'表示runtime. 这些运行时的表,包含流程实例 ...
- 中国移动OnetNet云平台 使用以太网传输数据流步骤
使用工具: 网络调试助手 链接:http://pan.baidu.com/s/1c06VC9E 密码:h0ys 1.选择TCP Client 2.输入IP 183.230.40.33 3.输入端口 ...
- Example 2 - contour plots
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" begin cdf_file = addfile("$N ...
- Java多线程系列 JUC线程池03 线程池原理解析(二)
转载 http://www.cnblogs.com/skywang12345/p/3509954.html http://www.cnblogs.com/skywang12345/p/351294 ...
- render总结
vue渲染组件的顺序是: 执行render函数-->没有render参数解析template参数内容-->没有template参数将el内html当做template解析 其中解析temp ...
- 20165101刘天野 2018-2019-2《网络对抗技术》Exp8 Web基础
20165101刘天野 2018-2019-2<网络对抗技术>Exp8 Web基础 1.实验内容 1.1 Web前端HTML (1)输入命令service apache2 start启动A ...
- Vim的map
linux系统下.vimrc文件(这个文件可以在家目录新建): 这个文件记录着vim的配置信息: 如: "显示行号 set number "键映射map “如按F5,在word的前 ...