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模块的更多相关文章

  1. python模块: hashlib模块, configparse模块, logging模块,collections模块

    一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...

  2. python中ConfigParse模块的用法

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 配置文件的格式是: [...]包含的叫section section 下有option=value这样的键值 ...

  3. 常用模块 - configparse模块

    一.简介 configparser模块在Python中是用来读取配置文件的,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节点(section),每个节可以有多个参数(键=值 ...

  4. python configparse模块&xml模块

    configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...

  5. Python进阶-XVV hashlib模块、configparse模块、logging模块

    1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...

  6. Python学习---重点模块之configparse

    configparse模块常用于生成和修改常见的配置文档 生成配置模块:用字典写 import configparser config = configparser.ConfigParser() co ...

  7. Python学习日记(二十八) hashlib模块、configparse模块、logging模块

    hashlib模块 主要提供字符加密算法功能,如md5.sha1.sha224.sha512.sha384等,这里的加密算法称为摘要算法.什么是摘要算法?它又称为哈希算法.散列算法,它通过一个函数把任 ...

  8. python学习-58 configparse模块

    configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...

  9. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

随机推荐

  1. Java之线程池(二)

    关于线程和线程池的学习,我们可以从以下几个方面入手: 第一,什么是线程,线程和进程的区别是什么 第二,线程中的基本概念,线程的生命周期 第三,单线程和多线程 第四,线程池的原理解析 第五,常见的几种线 ...

  2. Apache Shiro 使用手册(一)Shiro架构介绍(转发:http://kdboy.iteye.com/blog/1154644#bc2399255)

    一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 - 访问控制: 密码加密 ...

  3. js判断undefined类型,undefined,null, 的区别详细解析

    js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...

  4. 文件传输协议FTP

    之前已经了解了TCP/IP这种低级别的协议,还有一些网络协议包括文件传输(FTP,STP).阅读Usenet新闻组(NNTP).电子邮件发送(SMTP).从服务器上下载电子邮件(POP3.IMAP)等 ...

  5. ZOJ - 1505 Solitaire 【双向BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1505 题意 一个8 * 8 的棋盘上面有四个棋子 棋子可以上下左 ...

  6. 构造代码块、构造函数、this执行顺序

    一.构造函数 对象一建立就会调用与之对应的构造函数. 构造函数的作用:可以用于给对象进行初始化. 构造函数的小细节:当一个类中没有定义构造函数时,系统会默认给该类加一个空参数的构造函数:当在类中自定义 ...

  7. 【leetcode刷题笔记】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  8. curl常用指令

    curl 发送GET请求获取标准输出 curl -I 显示http请求头 curl -i 显示请求头及输出内容 curl xxx > xxx 将输出重定向到本地文件(本地文件无需已存在,一般不写 ...

  9. OJ的runtime error exit code对应SIGTERM代码

    Signal Name Number Description SIGHUP 1 Hangup (POSIX) SIGINT 2 Terminal interrupt (ANSI) SIGQUIT 3 ...

  10. codeforces 686B

    题意:给出一个序列,只允许进行相邻的两两交换,给出使序列变为非降序列的操作方案. 思路:关键点是操作次数不限,冒泡排序. #include<iostream> #include<cs ...