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'))      #此行必须有,因为文件是不可修改的,所以删除或修改操作必须是重写一次
  • 相关阅读:
    PHP 中 define() 和 const 定义常量时的区别
    tar 解压常用压缩文件格式命令大全
    JVM的GC策略
    守护线程(Daemon Thread)
    探索HashMap实现原理及其在jdk8数据结构的改进
    Java面试& HashMap实现原理分析
    Java分布式应用
    全面理解Java内存模型
    Java& Vector与ArrayList区别
    深入理解Java虚拟机&运行时数据区
  • 原文地址:https://www.cnblogs.com/Finance-IT-gao/p/10433053.html
Copyright © 2011-2022 走看看