zoukankan      html  css  js  c++  java
  • 函数和常用模块【day06】:configparser模块(七)

    本节内容

    1、简述

    2、配置文件格式

    3、创建配置文件

    4、读取配置文件

    5、增删该查语法

    一、简述

      在很多情况下,我们都需要修改配置文件,但是,有些配置文件,如mysql数据库的配置文件怎么修改呢?我们今天就来写一下,用于生产和修改常见配置文件的模块:configparser。

    二、配置文件格式

    1、配置文件格式

    [DEFALUT]
    compressionlevel = 9
    serveraliveinterval = 45
    compression = yes
    forwardx11 = yes
    
    [bitbucket.org]
    user = hg
    
    [topsecret.server.com]
    host port = 50022
    forwardx11 = no
    

    三、创建配置文件

    1、创建配置文件

    说明:其实有的时候我们很少创建,除非是用系统管理,一般直接修改就可以了,但是还是要掌握的。

    代码如下:

    import  configparser   #导入configparser模块
    
    #创建一个对象
    config = configparser.ConfigParser()
    #配置默认全局配置组
    config["DEFALUT"] = {"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'
    topsecret["ForwardX11"] = 'no'
    #给全局配置组赋值
    config["DEFALUT"]["ForwardX11"] = "yes"
    #操作完毕,把配置的内容写入一个配置文件中
    with open("example.ini","w") as configfile:
        config.write(configfile)
    

    四、读取配置文件

    1、读取配置组

    >>> import  configparser
    >>> config = configparser.ConfigParser()
    >>> config.sections()    #不读取配置文件,组名列表为空
    []
    >>> config.read("example.ini")  #读取配置文件,返回配置文件名
    ['example.ini']
    >>> config.sections()  #返回除默认配置组的其他组名
    ['bitbucket.org', 'topsecret.server.com']
    >>> config.defaults()   #读取默认配置组,并返回有序字典
    OrderedDict([('compressionlevel', '9'), ('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'yes')])
    

    2、组名是否存在

    >>> 'bitbucket.org' in config   #组名存在
    True
    >>> 'zhangqigao.org'  in config   #组名不存在
    False
    

    3、读取组内的值

    >>> config["bitbucket.org"]["User"]  #读取"bitbucket.org"配置组中的值
    'hg'
    >>> config["DEFAULT"]["Compression"]  #读取默认配置组中的值
    'yes'
    >>> topsecret = config['topsecret.server.com']  #把配置组赋给一个对象
    >>> topsecret['ForwardX11']   #通过对象获取值
    'no
    

    4、 循环获取组内的key值

    >>> for key in config["bitbucket.org"]:  #循环打印bitbucket.org组下的key值
    ...     print(key)
    ...
    #输出,只打印默认组和bitbucket.org组的key值
    user
    compressionlevel
    serveraliveinterval
    compression
    forwardx11
    >>> for key in config["topsecret.server.com"]:#循环打印topsecret.server.com组下的key值
    ...     print(key)
    ...
    #输出,只打印默认组和topsecret.server.com组的key值
    host port
    forwardx11
    compressionlevel
    serveraliveinterval
    compression
    

     注:默认组是全局的,所以循环遍历key值时,会遍历从默认组和需要遍历的组一起遍历出来。

    五、configparser增删改查语法

    1、配置文件名i.cfg

    [DEFAULT]
    k1 = v1
    k2 = v2
    
    [section1]
    k3 = v3
    k4:v4
    
    [section2]
    k5 = 5
    

    2、读i.cfg

    import configparser
    
    config = configparser.ConfigParser()
    config.read("i.cfg")
    sec = config.sections()
    print(sec)
    #输出
    ['section1', 'section2']
    
    options = config.options("section2")  #返回默认组和section2组的key值
    print(options)
    #输出
    ['k5', 'k1', 'k2']
    
    item_list = config.items("section2")   #返回默认组和section2组的key-value值
    print(item_list)
    #输出
    [('k1', 'v1'), ('k2', 'v2'), ('k5', '5')]
    
    val1 = config.get("section2","k1")   #获取section2组中k1对应的值,是否可取是按照上面返回的列表
    print(val1)
    #输出
    v1
    
    val2  = config.getint("section2","k5")  #返回section2中k5的值,这个值返回的int类型的
    print(val2)
    #输出
    5
    

    3、改写i.cfg

    ①删除section和option

    import configparser
    
    config = configparser.ConfigParser()
    
    config.read("i.cfg")
    config.remove_option("section1","k3")  #删除section1组下的k3
    config.remove_section("section2")   #删除section2组
    with open("i.cfg2","w") as f:   #重新写入一个文件
        config.write(f)
    
    #输出,写入文件的内容
    [DEFAULT]
    k1 = v1
    k2 = v2
    
    [section1]
    k4 = v4
    

    ②添加section

    import configparser
    
    config = configparser.ConfigParser()
    config.read("i.cfg")
    sec = config.has_option("section2","k5")  #是否存在section2组内有k5
    print(sec)
    #输出
    True
    
    sec = config.has_section("zhangqigao")  #是否存在zhangqigao组
    print(sec)
    #输出
    False
    
    config.add_section("zhangqigao")  #添加section组zhangqigao
    
    config.add_section("zhangqigao")  #重新写入到一个配置文件中
    with open("i.cfg3","w") as f:
        config.write(f)
    

     ③添加或者设置option

    import configparser
    
    config = configparser.ConfigParser()
    config.read("i.cfg")
    
    config.set("zhangqigao","z","18")  #设置或者添加zhangqigao中option值
    
    with open("i.cfg3","w") as f:   #重新写入文件中
        config.write(f)
  • 相关阅读:
    ASP.NET Web API 控制器执行过程(一)
    ASP.NET Web API 控制器创建过程(二)
    ASP.NET Web API 控制器创建过程(一)
    ASP.NET Web API WebHost宿主环境中管道、路由
    ASP.NET Web API Selfhost宿主环境中管道、路由
    ASP.NET Web API 管道模型
    ASP.NET Web API 路由对象介绍
    ASP.NET Web API 开篇示例介绍
    ASP.NET MVC 视图(五)
    ASP.NET MVC 视图(四)
  • 原文地址:https://www.cnblogs.com/luoahong/p/9890323.html
Copyright © 2011-2022 走看看