zoukankan      html  css  js  c++  java
  • re模块补充 configparse模块

    import re
    re.findall("(?:abc)+","abcabcabc")
    --->['abcabcabc']
    import configparser
    config = configparser.ConfigParser()   #创建配置文件,config = {}相当于一个字典
    
    config["default"] = {'ServerAliveInterval':45,
                         'Compression':'yes',
                         'CompressionLevel':'9'}
    
    config['bitbucket.org'] = {}
    config['bitbucket.org']['User'] = 'zsz'
    
    config['topsecret.server.com'] = {}
    topsecret = config['topsecret.server.com']
    topsecret['Host Port'] = '50022'
    topsecret['ForwardX11'] = 'no'
    
    with open('example.ini','w') as f:
        config.write(f)
    f.close()
    #----------------------------------------------增删改查------------------------------------------------------------
    #
    config = configparser.ConfigParser()
    config.read('example.ini')
    print(config.sections()) #得到所有的块
    --->['default', 'bitbucket.org', 'topsecret.server.com']
    print('bytebong.com' in config) #判断块中是否有bytebong.com
    --->False
    print(config['bitbucket.org']['User'])
    --->zsz
    print(config['default']['compression'])
    --->yes
    print(config['topsecret.server.com']['forwardx11'])
    --->no
    for key in config['bitbucket.org']: #取'bitbucket.org'下所有的键 print(key)
    --->user
    print(config.options('default')) #取键
    --->['serveraliveinterval', 'compression', 'compressionlevel']
    print(config.items('default')) #得到块下的键值对
    --->[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9')]
    print(config.get('default','compression')) #得到对应键的值
    --->yes

    #删,改,查 config.add_section('yuan') #增加块 config.set('yuan','k1','11111') #添加键值对 config.remove_section('topsecret.server.com') #删除块 config.remove_option('default','compression') #删除块下的键值对 config.write(open('example.ini','w')) #这么写不用关闭文件
     
  • 相关阅读:
    flume未解之谜
    flume source,sinks类型官方翻译
    flume-event类
    flume课件(连)
    source监控端口,telnet写入此端口。sinks指定目录,文件以默认时间滚动生成
    linux命令拾遗
    nginx内置变量
    nginx.conf
    hive事物开启
    hiveHA
  • 原文地址:https://www.cnblogs.com/zhangsenzhen/p/9425324.html
Copyright © 2011-2022 走看看