• 配置文档格式
  •  [DEFAULT]
    ServerAliveInterval = 45
    Compression = yes
    CompressionLevel = 9
    ForwardX11 = yes [bitbucket.org]
    User = hg

    配置文档文件格式

  • 解析配置文件:查询
  •  #-*- coding:utf-8 -*-
    #解析配置文件
    import configparser
    config = configparser.ConfigParser()
    print(config.sections())#[],此时打印为空,因为还没有读文件呢
    config.read("example.ini")
    print(config.sections())#打印节点['bitbucket.org', 'topsecret.server.com']
    if 'bitbucket.org' in config: #判断该节点是否在配置文件中
    print('true')
    print(config['bitbucket.org']['User'])#hg,查询value值,操作方式和字典一样
    print(config['DEFAULT']['Compression'])#yes,查询value值,操作方式和字典一样
    topsecret = config['topsecret.server.com']
    print(topsecret['ForwardX11'])#no
    print(topsecret['Port'])# for key in config['bitbucket.org']:#用for循环打印,除打印除'bitbucket.org'内的key,也会打印出'default’中的key,
    # 'default'中的内容是默认每个节点模块共享的,这样可以避免重复写
    print(key)#user serveraliveinterval compression compressionlevel forwardx11

    解析配置文件

  • 其他增删改查语法
  •  import configparser
    config = configparser.ConfigParser()
    config.read('example.ini')
    secs = config.sections()#获取节点
    print(secs)#['bitbucket.org', 'topsecret.server.com']
    options = config.options('bitbucket.org')#获取某一节点下的KEY
    print(options)#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
    item_list = config.items('bitbucket.org')#获取某一节点下的KEY,value
    print(item_list)#[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
    val = config.get('bitbucket.org','serveraliveinterval')
    print(val)#45,str格式
    val = config.getint('bitbucket.org','serveraliveinterval')
    print(val)#45,int格式

  • import configparser
    config = configparser.ConfigParser()
    config.read('example.ini')
    #remove section
    # sec = config.remove_section('bitbucket.org')#移除'bitbucket.org'节点
    # print(sec)##True:移除成功,False:移除失败(内容不存在)
    # config.write(open('example.ini','w'))#写
    #Add section
    # sec = config.has_section('group1')#判断该节点是否存在
    # print(sec)#False:不存在,True:存在
    # sec = config.add_section('group1')#添加,如果已经存在,会报错
    # config.write(open('example.ini','w')) #add option
    # config.set('group1','K1','11111')#给group1节点添加内容,如果group1不存在,会报错
    # config.write(open('example.ini','w')) #remove option
    # sec = config.remove_option('group1','K1')#移除option
    # print(sec)#True:移除成功,False:移除失败(内容不存在)
    # config.write(open('example.ini','w'))

    remove,add

configparser模块——用于生成和修改常见配置文档的更多相关文章

  1. configparser模块来生成和修改配置文件

    1. 安装configparser模块 pip3 install configparser ##python2.7模块名为ConfigParser 2. 创建配置文件 import configpar ...

  2. python常用模块-配置文档模块(configparser)

    python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...

  3. configparser模块——配置文档

    configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...

  4. Python学习 :常用模块(四)----- 配置文档

    常用模块(四) 八.configparser 模块 官方介绍:A configuration file consists of sections, lead by a "[section]& ...

  5. 转!!Java代码规范、格式化和checkstyle检查配置文档

    为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...

  6. Java代码规范、格式化和checkstyle检查配置文档

    http://www.blogjava.net/amigoxie/archive/2014/05/31/414287.html 文件下载: http://files.cnblogs.com/files ...

  7. azkaban编译安装配置文档

    azkaban编译安装配置文档 参考官方文档: http://azkaban.github.io/azkaban/docs/latest/ azkaban的配置文件说明:http://azkaban. ...

  8. nginx 安全配置文档

    1.配置文档中有多处明确写出了nginx的配置文件路径,该路径是测试环境中的路径,线上系统的nginx配置文件与文档中所写的路径可能不一样,在进行相关配置时,应以线上配置文件的实际路径为准. 线上系统 ...

  9. tomcat 安全配置文档

    1.配置文档中使用$CATALINA_HOME变量声明为tomcat的安装目录并明确写出了tomcat的配置文件路径,此路径为测试环境的路径,线上系统对应配置文件的路径可能不一样,在进行相关配置时,应 ...

随机推荐

  1. filter和map的使 使得数组对象变数组

    let UnitList = this.paytypeData.filter( item => item.CheckBox === true ).map(axis => axis.Unit ...

  2. android中常见的设计模式有哪些?

    建造者模式 建造者模式最明显的标志就是Build类,而在Android中最常用的就是Dialog的构建,Notification的构建也是标准的建造者模式. 建造者模式很好理解,如果一个类的构造需要很 ...

  3. Volley与Picasso的对比

    Volley与Picasso的对比 想写一篇文章来对比一下Volley以及Picasso,有人或许会说了,Volley和Picasso的服务对象都不同,Picasso是专注于图片的下载以及处理,而Vo ...

  4. 单列表变量与字符串拆分的对照(SqlServer)

    最近遇到一个问题,在SQLServer中,需要根据用户传入的一系列ID值更新对应的记录.有两种方法,一种是将这些ID值使用逗号分隔,拼接成字符串传入,一种是以表变量的方式传入.最开始,我想当然的认为传 ...

  5. MySql5.7主从配置

    记录 环境:ubuntu16.04,mysql5.7 主机:192.168.1.240,192.168.1.241:241为Salve 1.安装mysql sudo apt-get install m ...

  6. 卓越管理的实践技巧(2)成功的委派任务 Setup for Successful Delegation

    Setup for Successful Delegation 前文卓越管理的秘密(Behind Closed Doors)最后一部分提到了总结的13条卓越管理的实践技巧并列出了所有实践技巧名称的索引 ...

  7. acid (数据库事务正确执行的四个基本要素的缩写)

    ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...

  8. 2018.3.5 Java语言基础与面向对象编程实践

    Java语言基础与面向对象编程实践 第一章 初识Java 1.Java特点 http://www.manew.com/blog-166576-20164.html Java语言面向对象的 Java语言 ...

  9. WINDOWS-API:关于线程 GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId

    {返回当前线程的虚拟句柄} GetCurrentThread: THandle; {返回当前线程 ID} GetCurrentThreadId: DWORD; {返回当前进程的虚拟句柄} GetCur ...

  10. springboot 内置tomcat maxPostSizs 无法设置

    ++++++++++++++++++ RT +++++++++++++++++ 如下代码方可解决: /** * tomcat配置类 * 解决post数据体大于2048kb无法接收问题 * 解决tomc ...