• python学习之老男孩python全栈第九期_day029知识点总结——configparser模快、logging模块


    一. configparser模块

    生成文档
    import configparser
    config = configparser.ConfigParser()
    config["DEFAULT"] = {'ServerAliveInterval': '45',
                          'Compression': 'yes',
                         'CompressionLevel': '9',
                         'ForwardX11':'yes'
                         }
    config['bitbucket.org'] = {'User':'hg'}
    config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
    with open('example.ini', 'w') as configfile:
       config.write(configfile)
    
    查找文件
    import configparser
    config = configparser.ConfigParser()
    #---------------------------查找文件内容,基于字典的形式
    print(config.sections())        #  []
    config.read('example.ini')
    print(config.sections())        #   ['bitbucket.org', 'topsecret.server.com']
    print('bytebong.com' in config) # False
    print('bitbucket.org' in config) # True
    print(config['bitbucket.org']["user"])  # hg
    print(config['DEFAULT']['Compression']) #yes
    print(config['topsecret.server.com']['ForwardX11'])  #no
    print(config['bitbucket.org'])          #<Section: bitbucket.org>
    for key in config['bitbucket.org']:     # 注意,有default会默认default的键
        print(key)
    print(config.options('bitbucket.org'))  # 同for循环,找到'bitbucket.org'下所有键
    print(config.items('bitbucket.org'))    #找到'bitbucket.org'下所有键值对
    print(config.get('bitbucket.org','compression')) # yes       get方法Section下的key对应的value
    
    增删改
    import configparser
    config = configparser.ConfigParser()
    config.read('example.ini')  # 读文件
    config.add_section('yuan')      # 增加section
    config.remove_section('bitbucket.org')      # 删除 section
    config.remove_option('topsecret.server.com',"forwardx11")       # 删除一个配置项
    config.set('topsecret.server.com','k1','11111')
    config.set('yuan','k2','22222')
    f = open('new2.ini', "w")
    config.write(f)     # 写进文件
    f.close()

    二. logging模块

    # 什么叫日志?
    # 日志:是用来记录用户行为 或者 代码的执行过程
    
    # logging
    # 我能够‘一键’控制
    # 排错的时候需要打印很多细节来帮助我排错
    # 严重的错误记录下来
    # 有一些用户行为 有没有错我都要记录下来
    import logging
    logging.debug('debug message')              # 低级别的:排错信息
    logging.info('info message')                # 正常信息
    logging.warning('warning message')          # 警告信息
    logging.error('error message')              # 错误信息
    logging.critical('critical message')        # 高级别的:严重错误信息

    logger对象配置

    import logging
    
    logger = logging.getLogger()
    # 创建一个handler,用于写入日志文件
    fh = logging.FileHandler('test.log',encoding='utf-8') 
    
    # 再创建一个handler,用于输出到控制台 
    ch = logging.StreamHandler() 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
    fh.setLevel(logging.DEBUG)
    
    fh.setFormatter(formatter) 
    ch.setFormatter(formatter) 
    logger.addHandler(fh) #logger对象可以添加多个fh和ch对象 
    logger.addHandler(ch) 
    
    logger.debug('logger debug message') 
    logger.info('logger info message') 
    logger.warning('logger warning message') 
    logger.error('logger error message') 
    logger.critical('logger critical message')
  • 相关阅读:
    在.netframework 4.5.2项目上集成identityserver4的登录功能
    Elasticsearch学习笔记之—测试查询分词器的分词结果
    asp.net core mysql 错误提示:Out of memory (Needed***
    Elasticsearch学习笔记之— excludes的高级用法
    正则表达式(.*?)
    WPF里实现imageButton的步骤
    Elasticsearch学习笔记之—wildcard、fuzzy、regexp、prefix
    Elasticsearch学习笔记之—数据范围查询
    工具小方法
    lambda表达式
  • 原文地址:https://www.cnblogs.com/lpgit/p/9420304.html
Copyright © 2020-2023  润新知