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

    #_author:star
    #date:2019/11/7
    # configparser 配置文件模块
    import configparser
    config=configparser.ConfigParser()
    # config['DEFAULT']={'ServerAliveInterval':'45',
    # 'Compress':'yes',
    # 'CompressionLevel':'9'}
    # config['bitbucket.org']={'user':'hg'}
    #
    # config['topsecret.server.com']={}
    # topsecret=config['topsecret.server.com']
    # topsecret['Host Part'] ='50022'
    #
    # with open('example.ini','w') as configfile:
    # config.write(configfile)
    config.read('example.ini')
    print(config.sections())#['bitbucket.org', 'topsecret.server.com'] 除了DEFAULT之外的其他内容
    #查看DEFAULT
    print(config.defaults())#OrderedDict([('serveraliveinterval', '45'), ('compress', 'yes'), ('compressionlevel', '9')])
    print('bitbucket.org' in config)#True

    print(config['bitbucket.org']['user'])#hg

    for key in config:
    print(key)
    # DEFAULT
    # bitbucket.org
    # topsecret.server.com
    print('----------------')
    for key1 in config['bitbucket.org']:
    print(key1)
    # user
    # serveraliveinterval
    # compress
    # compressionlevel
    print('-------------')
    config.remove_section('topsecret.server.com')#删除某一个块
    print(config.has_section('topsecret.server.com'))#False
    print(config.has_section('bitbucket.org'))#True

    config.set('DEFAULT','ServerAliveInterval','66')


    config.remove_option('DEFAULT','serveraliveinterval')#删除块下的某一个键值对
    config.write(open('r.cfg','w'))#无论怎么修改文件,都要最后重写文件,因为文件一旦生成,就无法修改
    Output:



  • 相关阅读:
    MR中简单实现自定义的输入输出格式
    简单实现CombineFileInputFormat
    提高mapreduce性能的七点建议
    MR中使用sequnceFIle输入文件
    Hive中使用LZO
    JVM启动参数详解 (转)
    ubuntu12.04中shell脚本无法使用source的原因及解决方法
    hadoop 错误
    poj 3211 Washing Clothes
    hdu 3535 AreYouBusy
  • 原文地址:https://www.cnblogs.com/startl/p/11811292.html
Copyright © 2011-2022 走看看