zoukankan      html  css  js  c++  java
  • configpraser模块

    configpraser配置文件,example.conf

    [data] #节点
    username = Jason
    password = 123
    
    [public]
    comment = stuff
    public = True
    pi = 3.1415926
    test_int = 9
    

    程序实例

    import configparser
    config = configparser.ConfigParser()
    config.read("example.conf", "utf-8")
    ret = config.sections() #获取所有的节点
    print(ret) #['data', 'public']
    print(config.items('data'))#获取指定节点下的所有键值对
    #[('username', 'Jason'), ('password', '123')]
    print(config.options("public")) #获取指定节点下的所有键
    #['comment', 'public', 'pi']
    print(config.get('data', 'username')) #获取指定节点下指定key的值
    #Jason
    res1 = config.getint('public','test_int')#获取指定节点下key值,必须为整数否则报错
    res2 = config.getfloat('public','pi')#获取指定节点下key值,必须为浮点数否则报错
    res3 = config.getboolean('public','public')#获取指定节点下key值,必须为布尔值否则报错
    print(res1, res2, res3)
    check = config.has_section("data") #检查节点
    print(check)
    #True
    

    添加节点

    config.add_section('local') #添加节点
    config.write(open('example.conf','w')) #
    ret = config.sections()
    print(ret)
    #['global', 'public', 'local']
    

    删除节点

    config.remove_section('local') #删除节点
    config.write(open('example.conf','w'))#重新写入文件
    ret = config.sections()
    print(ret)
    #['global', 'public']
    

    检查,删除,设置指定组内的键值对

    #检查
    check = config.has_option('public','comment')#检查节点下的某个键,返回布尔值
    print(check)
    输出:
    True
    
    #删除
    config.remove_option('global','workgroup')
    config.write(open('example.txt','w'))
    ret = config.options('global')
    print(ret)
    #输出:
    ['security', 'maxlog']
    
    
    #设置指定节点内的键值对
    ret1 = config.get('global','maxlog')
    print(ret1)
    config.set('global','maxlog','100')
    config.write(open('example.txt','w'))
    ret2 = config.get('global','maxlog')
    print(ret2)
    
    #输出:
    50
    100
    
    configpraser设置多个配置文件然后遍历,这样做Frabic作业分组问题简直超级方便,but遇到了问题!!!
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # __author__:JasonLIN
    import os
    import sys
    import configparser
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    sys.path.append(BASE_DIR)
    db_path = os.path.join(BASE_DIR, "db")
    config_info = configparser.ConfigParser()
    group_dic = {}
    group_list = os.listdir(db_path)
    print(group_list)
    for group in group_list:
        group_path = os.path.join(db_path, str(group))
        config_info.read(group_path, "utf-8")
        ip_list = config_info.sections()
        print("ip list>>", ip_list)
        group_dic.update({group: ip_list})
        print(group)
        print("group dict", group_dic)
    

    输出结果

    ['111', '33']
    ip list>> ['192.168.1.1']
    111
    group dict {'111': ['192.168.1.1']}
    ip list>> ['192.168.1.1', '192.168.229.128', '192.168.229.131']
    33
    group dict {'111': ['192.168.1.1'], '33': ['192.168.1.1', '192.168.229.128', '192.168.229.131']}
    
    Process finished with exit code 0
    

    两个配置文件
    33

    [192.168.229.128]
    user = jason
    pwd = 123
    ports = 22
    
    [192.168.229.131]
    user = jason
    pwd = 123
    ports = 33
    

    111

    [192.168.1.1]
    user = dog
    pwd = 123
    ports = 88
    

    补充
    在config.read("file_path"),前加config.clear()即可解决上述问题

  • 相关阅读:
    (转)mysql 中的 latch锁和Tlock(事务锁), DML加锁规则,以及死锁分析
    改变主库sync_binlog,减小主从同步延时
    windows10上使用SourceInsight阅读mysql源码
    centos6.5安装systemstap
    centos6.5编译调试mysql-5.7.18
    事物特性
    Union和union all区别?
    Join(inner、left、right)的区别?
    hashhashmaphashTablehashSet
    String、StringBuffer、StringBuilder区别
  • 原文地址:https://www.cnblogs.com/Jason-lin/p/7636272.html
Copyright © 2011-2022 走看看