来看一个好多软件的常见文档格式如下:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

如果想用python生成一个这样的文档怎么做呢?

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'<br>
with open('example.ini', 'w') as configfile:
config.write(configfile)
import configparser

config = configparser.ConfigParser()

#---------------------------------------------查
print(config.sections()) #[] config.read('example.ini') print(config.sections()) #['bitbucket.org', 'topsecret.server.com'] print('bytebong.com' in config)# False print(config['bitbucket.org']['User']) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no for key in config['bitbucket.org']:
print(key) # user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11 print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')] print(config.get('bitbucket.org','compression'))#yes #---------------------------------------------删,改,增(config.write(open('i.cfg', "w"))) config.add_section('yuan') config.remove_section('topsecret.server.com')
config.remove_option('bitbucket.org','user') config.set('bitbucket.org','k1','11111') config.write(open('i.cfg', "w")) 增删改查

python configparser模块的更多相关文章

  1. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  2. python -ConfigParser模块讲解

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  3. python ConfigParser模块 配置文件解析

    ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...

  4. 【python】python configparser模块

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

  5. python configparser模块详解

    此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...

  6. Python - configParser模块学习

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  7. python ConfigParser 模块

    ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...

  8. Python configparser模块操作代码实例

    1.生成配置文件 ''' 生成配置文件 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知 ...

  9. python 常用模块之ConfigParser

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在Python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser, Python C ...

随机推荐

  1. ubuntu 'Unable to correct problems, you have held broken packages' 错误

    在用apt 安装软件时,有时会用国内的源以加快下载速度. 但是在使用ubuntu 14.04的过程中,这一过程可能会导致错误“Unable to correct problems, you have ...

  2. Hibernate一对一外键双向关联(Annotation配置)

    如上图所示:一个学生有一个学生证号,一个学生证号对应一名学生.在Hibernate中怎么用Annotation来实现呢? 学生类,主键是id:学生证的主键也是Id: Student.java pack ...

  3. SpringMVC学习笔记(六)

    一.SpringMVC文件的上传 1.1.需要导入两个jar包 1.2在SpringMVC配置文件中加入 <!-- upload settings --> <bean id=&quo ...

  4. Java 实现多线程的两种方式

    1:继承Therad类2:实现Runnable 接口 1.继承Thread类实现多线程继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实 ...

  5. HBase学习

    记录HBase的学习过程.之后会陆续添加内容. 阅读hbase的博客,理解hbase是什么.推荐博文: 1,HBase原理,基础架构,基础概念 2,HBase超详细介绍 --------------- ...

  6. 用原生js写碰撞变色效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. MVP模式和MVVM模式

    MVP模式 模型-视图-表示器,也就是MVP模式.是mvc模式的一种衍生模式,专注于改进表示逻辑. 与MVC不同,来自view的调用将委托给presenter(表示器),表示器通过接口与view对话. ...

  8. Perl爬取铁路违章旅客信息

    #! /usr/bin/perl use strict; use Encode qw(encode decode); binmode(STDIN,":encoding(utf8)" ...

  9. PLSQL登录数据库 报ORA -12154的诡异问题

    https://q.cnblogs.com/q/89420/ 现象: 1.机器上先后安装了oracle两个版本的client.在装第一个client后,plsql可以顺利连接数据库a并登录. 2.安装 ...

  10. myeclipse ctrl + 鼠标单击 出现 source not found

    有时候我们下载来目录中有一个src文件夹,里面是源代码,而不是打包好的jar或zip文件.src目录下的源代码是按照包结构存放的,比如a.java的第一行是package test; 那么在src/t ...