zoukankan      html  css  js  c++  java
  • python之模块配置文件ConfigParser(在python3中变化较大)

    # -*- coding: utf-8 -*-
    #python 27
    #xiaodeng
    #python之模块ConfigParser(在python3中为configparser)
    #特别注意:python3和python2关于该模块的功能用法有很大的不同.
    
    
    
    #配置文件解析器
    import ConfigParser,os
    
    
    #初始化一个配置文件对象
    config=ConfigParser.ConfigParser()
    
    
    #增加一个section
    config.add_section('Section01')
    config.add_section('Section02')
    
    
    #给section增加属性键值对
    config.set('Section01','name','xiaodeng')#set(section, option, value)
    config.set('Section01','age','28')
    config.set('Section01','bar','python')
    config.set('Section01', 'an_int', '15')
    config.set('Section01', 'a_bool', 'true')
    config.set('Section01', 'a_float', '3.1415')
    config.set('Section01', 'baz', 'fun')
    config.set('Section01', 'bar', 'fengMei')
    config.set('Section01', 'foo', '%(bar)s is %(baz)s!')
    config.set('Section01','age','25')
    config.set('Section02', 'bar', 'Python')
    config.set('Section02','bar','python')
    
    
    #写入文件
    with open('test.cfg','wb') as configFile:
        config.write(configFile)
    
    
    #读取cfg文件
    config=ConfigParser.ConfigParser()
    config.read('test.cfg')#ConfigParser.ConfigParser instance
    
    
    #取值
    print config.getfloat('Section01','a_float')#getfloat(section, options)
    print config.getint('Section01','an_int')#getint(section, options)
    print config.getboolean('Section01','a_bool')#getboolean(section, options)
    
    
    
    #返回一个list形式的元组;items(section, raw=False, vars=None)
    print config.items('Section01')
    #[('name', 'xiaodeng'), ('age', '25'), ('bar', 'fengMei'), ('an_int', '15'), ('a_bool', 'true'), ('a_float', '3.1415'), ('baz', 'fun'), ('foo', 'fengMei is fun!')]
    
    
    #取所有的section名字
    print config.sections()#['Section01', 'Section02']
    
    
    #获取所有的配置表名字key
    print config.options('Section01')#['name', 'age', 'bar', 'an_int', 'a_bool', 'a_float', 'baz', 'foo']
    
    
    #删除指定Section选项下内容
    config.remove_section('Section02')#测试时查看文件中还是存在Section02,但是用指令查看sections的名字时只有Section01
    
    
    #get(section, option, raw=False, vars=None)
    #在指定的section选项中查找特定option的value值
    #注意:这里默认查找的是最后一个相同option的值
    print config.get('Section01','bar')
    print config.get('Section01','age')
    print config.get('Section01','bar')
    
    
    
    '''
    ConfigParser -- responsible for parsing a list of
                        configuration files, and managing the parsed database.
        
            methods:
        
            __init__(defaults=None)
                create the parser and specify a dictionary of intrinsic defaults.  The
                keys must be strings, the values must be appropriate for %()s string
                interpolation.  Note that `__name__' is always an intrinsic default;
                its value is the section's name.
        
            sections()
            #取所有的section名字
                return all the configuration section names, sans DEFAULT
        
            has_section(section)
                return whether the given section exists
        
            has_option(section, option)
                return whether the given option exists in the given section
        
            options(section)
            #获取所有的配置表名字key
                return list of configuration options for the named section
        
            read(filenames)
                read and parse the list of named configuration files, given by
                name.  A single filename is also allowed.  Non-existing files
                are ignored.  Return list of successfully read files.
        
            readfp(fp, filename=None)
                read and parse one configuration file, given as a file object.
                The filename defaults to fp.name; it is only used in error
                messages (if fp has no `name' attribute, the string `<???>' is used).
        
            get(section, option, raw=False, vars=None)
                return a string value for the named option.  All % interpolations are
                expanded in the return values, based on the defaults passed into the
                constructor and the DEFAULT section.  Additional substitutions may be
                provided using the `vars' argument, which must be a dictionary whose
                contents override any pre-existing defaults.
        
            getint(section, options)
                like get(), but convert value to an integer
        
            getfloat(section, options)
                like get(), but convert value to a float
        
            getboolean(section, options)
                like get(), but convert value to a boolean (currently case
                insensitively defined as 0, false, no, off for False, and 1, true,
                yes, on for True).  Returns False or True.
        
            items(section, raw=False, vars=None)
                return a list of tuples with (name, value) for each option
                in the section.
        
            remove_section(section)
                remove the given file section and all its options
        
            remove_option(section, option)
                remove the given option from the given section
        
            set(section, option, value)
                set the given option
        
            write(fp)
                write the configuration state in .ini format
    
            add_section(self, section)
                method of ConfigParser.ConfigParser instance
                
            Create a new section in the configuration.
    '''
  • 相关阅读:
    C++ 引用做左值
    C++ 引用本质的详解
    C++ 引用基础
    C语言错误 指针的类型错误
    C++ c++与C语言的区别(三目运算符,const修饰符)
    C++ c++与C语言的区别(struct类型的加强,函数-变量类型加强,bool类型)
    C++ c++与C语言的区别(实用性增强,register关键字增强,全局变量检测增强)
    C++ c++初识
    C语言 Linux内核链表(企业级链表)
    C语言 结构体中属性的偏移量计算
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/4948601.html
Copyright © 2011-2022 走看看