zoukankan      html  css  js  c++  java
  • python课堂整理31----configparser模块

    一、功能:为配置文件开发

    创建一个配置文件:

    import configparser
    config = configparser.ConfigParser()
    config["DEFAULT"] = {
        "ServerAliveInterval": '45',
        'Compression': 'yes',
        'CompressionLevel':'9'
    }
    config['bitbuckket.org'] = {} #创建一个空字典
    config['bitbuckket.org']['User'] = 'jinling'
    config['topsecret.server.com'] = {}   #创建一个空字典
    topsecret = config['topsecret.server.com']
    topsecret['Host Port'] = '50022'
    topsecret['ForwardX11'] = 'no'
    config['DEFAULT']['ForwardX11'] = 'yes'   #向DEFAULT块添加一个新的键值对
    with open('example.ini', 'w') as configfile:
        config.write(configfile)
    

    -----------------------------增删改查-----------

    #查

    import configparser
    config = configparser.ConfigParser()  #重要
    print(config.sections()) # 查看文件里的块(除DEFAULT) 这里因为没有读入文件,所以为空
    config.read('example.ini')  #读入之前创建的配置文件
    print(config.sections())    # 查看文件里的块(除DEFAULT)
    print('bitbuckket.org' in config) #判断这个快是否在文件中
    print(config['bitbuckket.org']['user']) #获取bitbuckket.org下user的值,这里user不区分大小写
    print(config['DEFAULT']['compression']) #获取DEFAULT下compression的值
    

    import configparser
    config = configparser.ConfigParser()  #重要
    config.read('example.ini')#读入之前创建的配置文件
    for key in config['bitbuckket.org']:   #会把DEFAULT下的键也循环打印出来
        print(key)
    print(config.options('bitbuckket.org')) #获取键,并放在一个列表(同样把默认的键也放了进去)
    print(config.items('bitbuckket.org'))#获取键和值,并放到一个列表
    print(config.get('bitbuckket.org', 'compression')) #到bitbuckket.org快下获取compression的值
    

      

    #增

    import configparser
    config = configparser.ConfigParser()  #重要
    config.read('example.ini')#读入之前创建的配置文件
    config.add_section('yuan') #添加一个块
    config.set('yuan', 'k1','111') #向这个块添加一个键值对、
    
    
    config.write(open('jin.cfg', 'w'))  #把修改后的内容写入文件,可以同名覆盖
    

      

    #删

    import configparser
    config = configparser.ConfigParser()  #重要
    config.read('example.ini')#读入之前创建的配置文件
    # config.add_section('yuan') #添加一个块
    # config.set('yuan', 'k1','111') #向这个块添加一个键值对、
    config.remove_section('topsecret.server.com')  #删除这个块,连同下面的所有键值对
    
    config.write(open('example.ini', 'w'))  #把修改后的内容写入文件,可以同名覆盖
    

      

    import configparser
    config = configparser.ConfigParser()  #重要
    config.read('example.ini')#读入之前创建的配置文件
    # config.add_section('yuan') #添加一个块
    # config.set('yuan', 'k1','111') #向这个块添加一个键值对、
    # config.remove_section('topsecret.server.com')  #删除这个块,连同下面的所有键值对
    
    config.remove_option('bitbuckket.org', 'user') #删除某个键值对
    config.write(open('example.ini', 'w'))  #把修改后的内容写入文件,可以同名覆盖
    

      

     

    一个奋斗中的产品小白
  • 相关阅读:
    POJ 3308 Paratroopers
    POJ 3228 Gold Transportation
    POJ 4786 Fibonacci Tree
    POJ 2987 Firing
    Models——英语学习小技巧之四
    Linux编程环境介绍(3) -- linux下的c/c++程序开发
    怎样使用Markdown
    windows系统中的dll的作用详细解释
    解决ListView 和ScroolView 共存 listItem.measure(0, 0) 空指针
    网页添加背景音乐
  • 原文地址:https://www.cnblogs.com/dabai123/p/11393567.html
Copyright © 2011-2022 走看看