zoukankan      html  css  js  c++  java
  • (十八)configparser模块

    configparser模块一般是用来处理配置文件的,如:

    [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对象
    
    config["DEFAULT"] = {'ServerAliveInterval': '45',  #第一个[DEFAULT]块,像字典一样去定义;[DEFAULT]块是默认的块,有特殊之处,见后面
                         'Compression': 'yes',
                         'CompressionLevel': '9'}
    
    config['bitbucket.org'] = {}                       #第二个[bitbucket.org]块
    config['bitbucket.org']['User'] = 'hg'
    
    config['topsecret.server.com'] = {'Port':'50022',  #第三个[topsecret.server.com]块
                                        'ForwardX11':'no'}
    
    with open('test.ini', 'w') as configfile:          #自定义写入的文件
        config.write(configfile)

    查看

    import configparser
    config = configparser.ConfigParser()                
    
    config.read('test.ini')                       #读取文件
    
    print(config.sections())   #['bitbucket.org', 'topsecret.server.com']    #sections()就是所有的块,不包括DEFAULT默认块
    
    print('bytebong.com' in config)# False                                   #不存在就是false
    
    print(config['bitbucket.org']['User']) # hg                              #存在,拿到键user对应的值hg
    
    print(config['DEFAULT']['Compression']) #yes                             #存在,拿到键compression对应的值yes                          
    
    for key in config['bitbucket.org']:  #user serveraliveinterval compression compressionlevel    #除了user,还拿到了DEFAULT默认块里的键
        print(key)
    结果:

    ['bitbucket.org', 'topsecret.server.com']
    False
    hg
    yes
    user
    serveraliveinterval
    compression
    compressionlevel

    查看还有三个方法:

    print(config.options('bitbucket.org'))                             #options()拿到键,同样包括默认块
    #['user', 'serveraliveinterval', 'compression', 'compressionlevel']
    print(config.items('bitbucket.org'))                               #items()拿到键值对,同样包括默认块
    #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('user', 'hg')]
    print(config.get('bitbucket.org','compression'))                   #get()拿到一个块中的一个键对应的值
    #yes

    所以DEFAULT块是默认块,其他的每一个块中都包括默认块中的键值对

    增删改

    config.add_section('yuan')                        #新增yuan块
    config.remove_section('topsecret.server.com')   #删除topsecret.server.com块
    config.set('bitbucket.org','user','admin')     #修改bitbucket.org块的user键对应的值为admin
    config.set('bitbucket.org','passwd','123456')  #新增bitbucket.org块的passwd键,值为123456
    config.remove_option('bitbucket.org','user')    #删除bitbucket.org块的user键值对
    config.set('yuan','k1','11111')                  #新增yuan块的k1键,值为11111
    
    config.write(open('test123.ini', "w"))           #最后要写入文件,可以是读取时的文件(即覆盖),也可以是新增的另一个文件

    最后被修改后的test123.ini文件:

    [DEFAULT]
    serveraliveinterval = 45
    compression = yes
    compressionlevel = 9
    
    [bitbucket.org]
    passwd = 123456
    
    [yuan]
    k1 = 11111
  • 相关阅读:
    使用ssh公钥实现ssh免密码登录
    如何定义领域模型(概念模型)
    17.python字符编码检测——chardet
    21.python对象的浅拷贝和深拷贝
    15.序列化python对象
    18.python的打包和发布
    16.python的网络编程
    13.python的文件操作
    linux下python、django框架的配置
    14.python的xml操作
  • 原文地址:https://www.cnblogs.com/xulan0922/p/10306841.html
Copyright © 2011-2022 走看看