zoukankan      html  css  js  c++  java
  • configparser配置文件模块

    创建配置文件:

    import configparser
    config = configparser.ConfigParser()
    config['DEFAULT'] = {'server':'45',
                         'compression':'yes',
                         'compressionLevel':'9'}
    config['bucket.org'] = {'User':'hg',
                            'age':'23'}
    config['top'] = {'host':'522',
                     'forward':'no'}
    config.write(open('example.ini','w'))       #用config给example.ini里写东西
    # with open('example.ini','w') as configfile: #同上
    #     config.write(configfile)

    查看配置文件内容:

    config.read('example.ini')         #读配置文件,先读才能进行以下打印
    
    print(config.sections())           #打印默认块以外的其他块名
    print(config.default_section)      #打印默认块的名字
    print(config.defaults())           #打印默认块的内容
    print(config['bucket.org']['user'])#打印user的内容
    for key in config:                   #打印配置文件中各个块名
        print(key)
    for key in config['bucket.org']:   #打印'bucket.org'块所含选项的名字
        print(key)

    删除/修改操作:

    config.read('example.ini')                 #先读到才能进行删除或修改操作
    
    config.remove_option('bucket.org','age')   #删掉budget.org块中的age项
    config.remove_section('top')               #删掉top块
    print(config.has_section('bucket.org'))    #判断是否包含这个块
    config.set('bucket.org','user','app')      #修改budget.org块中user项的值
    
    config.write(open('example.ini','w'))      #此行必须有,因为文件是不可修改的,所以删除或修改操作必须是重写一次
  • 相关阅读:
    ActiveMQ持久化消息
    JAXB XML到java object的转换
    Jackson JSON Processor
    Spring JDBC入门
    Quatz入门
    Jersey 入门与Javabean
    记安装ubuntu server和一些程序
    libevent 入门教程:Echo Server based on libevent(转)
    Linux Eclipse 运行Protobuf
    精神状态: Confused
  • 原文地址:https://www.cnblogs.com/Finance-IT-gao/p/10433053.html
Copyright © 2011-2022 走看看