zoukankan      html  css  js  c++  java
  • python configparser 模块

    configparser 模块是用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

    示例配置文件

    [DEFAULT]
    ServerAliveInterval = 45
    Compression = yes
    CompressionLevel = 9
     
    [test1.net.cn]
    user = test
    host = localhost
    port = 8080
     
    [top.net.cn]
    test_port = 9090
    user = root
    port = 2202

    1、生成配置文件

    import configparser
    config = configparser.ConfigParser()
    config["DEFAULT"] = {
        'ServerAliveInterval':45,
        'Compression':'yes',
        'CompressionLevel':9
    }
    config['test1.net.cn'] = dict() # 使某一块配置等于一个字典
    config['test1.net.cn']['user'] = 'test' #使用键值对的方式添加一行配置
    config['test1.net.cn']['host'] = 'localhost'
    config['test1.net.cn']['port'] = '8080' #所有的值都以字符串的方式传进去
    
    config['top.net.cn'] = dict() 
    top = config['top.net.cn']
    top['test_port'] = '9090'
    top['user'] = 'root'
    top['port'] = '2202'
    
    with open('test.ini','w') as configfile:
        config.write(configfile) # 最后一定要写入到配置文件

    #####################

    [DEFAULT]
    serveraliveinterval = 45
    compression = yes
    compressionlevel = 9

    
    

    [test1.net.cn]
    user = test
    host = localhost
    port = 8080

    
    

    [top.net.cn]
    test_port = 9090
    user = root
    port = 2202

    #####################

    2、读取配置文件

    import configparser
    config = configparser.ConfigParser()
    config.read('test.ini')
    
    
    print ('top.net.cn' in config) # 判断一个sections是否在配置文件中
    for key in config: # 遍历所有的sections
        print (key)
    print (config.sections()) # 直接打印所有的sections
    print (config.defaults()) # 获取DEFAULT的特殊方法,获取那个单独的sections,都会带有DEFALUT的值。
    print (config['test1.net.cn']['port']) # 获取单独的某一个记录
    print (config.items('top.net.cn')) # 获取一个sections的记录
    print (config.options('test1.net.cn')) # 获取test1.net.cn的sections,其中也包括DEFALUT的值
    print (config.items('top.net.cn')) #获取top.net.cn的所有记录
    var = config.get('top.net.cn','test_port') #获取某一个sections下的一个key的值
    print (var)
    
    # True
    # DEFAULT
    # test1.net.cn
    # top.net.cn
    # ['test1.net.cn', 'top.net.cn']
    # OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9')])
    # 8080
    # [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('test_port', '9090'), ('user', 'root'), ('port', '2202')]
    # ['user', 'host', 'port', 'serveraliveinterval', 'compression', 'compressionlevel']
    # [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('test_port', '9090'), ('user', 'root'), ('port', '2202')]
    # 9090

    3、修改配置文件

    import configparser
    config = configparser.ConfigParser()
    config.read('test.ini')
    
    # 删除已经存在的sections top.net.cn。
    remove_sections = config.remove_section('top.net.cn')
    
    # 如果一个sections存在,再次添加就会出现异常。所以我现在先删除,然后再添加。
    # add_section = config.has_section('leslie')
    config.remove_section('leslie')
    
    # 添加一个不存在的sections。
    add_section = config.add_section('leslie')
    
    # 对已经存在的sections中添加记录。
    config.set('leslie','name','xikang')
    
    # 删除已经在sections中的一条记录。
    config.remove_option('test1.net.cn','port')
    
    # 最后一定要把这些修改写到文件中。
    config.write(open('test.ini','w'))
  • 相关阅读:
    在Salesforce中创建Approval Process
    用C#基于WCF创建TCP的Service供Client端调用
    用 C# 实现一个简单的 Rest Service 供外部调用
    在Salesforce中将 Decimal 数据转换成美元格式
    在Asp.Net MVC中PartialView与EditorFor和DisplayFor的区别
    在Salesforce中对某一个Object添加自定义的Button和Link
    【LeetCode】227. Basic Calculator II
    【LeetCode】226. Invert Binary Tree
    【LeetCode】225. Implement Stack using Queues
    【LeetCode】224. Basic Calculator
  • 原文地址:https://www.cnblogs.com/qikang/p/8647391.html
Copyright © 2011-2022 走看看