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')) #这么写不用关闭文件
     
  • 相关阅读:
    subset子集全排序问题
    第15章动态规划------算法导论
    内存管理思维导图------现代操作系统(第四版)
    进程与线程思维导图------现代操作系统(第四版)
    现代操作系统
    GHUnitiOS 单元测试(带UI界面)下载地址
    iOS + webSocket 通讯
    iOS 甘特图的实现
    NSDate根据日期获得当前是周几,以及一年内的第几周,以及一个月内得第几周
    Swift 中文文档
  • 原文地址:https://www.cnblogs.com/zhangsenzhen/p/9425324.html
Copyright © 2011-2022 走看看