20 python 初学(logging模块)

学习网站:https://www.cnblogs.com/yuanchenqi/articles/5732581.html

 

logging 模块:

# _author: lily
# _date: 2019/1/14
import logging

# logging.debug('debug message')
# logging.info('info message')
# logging.error('error message')
# logging.warning('warning message')
# logging.critical('critical message')

# logging.basicConfig(level=logging.DEBUG,
#                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
#                     datefmt='%a,%d %b %Y %H:%M:%S',
#                     filename='test.log',
#                     filemode='w')
#
# #Mon,14 Jan 2019 23:23:11 learn_of_logging_module.py[line:17] DEBUG debug message
# logging.debug('debug message')
# logging.info('info message')
# logging.error('error message')
# logging.warning('warning message')
# logging.critical('critical message')

logger = logging.getLogger()
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log')   # 文件对象
# 在创建一个handler,用于输出到控制台
ch = logging.StreamHandler()  # 屏幕对象

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setFormatter(formatter)
ch.setFormatter(formatter)

logger.addHandler(fh)
logger.addHandler(ch)

logger.setLevel(logging.DEBUG)

logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
View Code

 

configparser块:

 

# _author: lily
# _date: 2019/1/15

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
    config.write(configfile)
View Code

 

 

re 模块:

import re

ret = re.findall('adc+?', 'adccc')   # ['abd']
print(ret)


ret = re.findall('(?P<name>\w{2})(?P<age>\d{2})', 'djkwif728372')
print(ret)    # [('if', '72'), ('83', '72')]

ret = re.search('(?P<name>\w{2})(?P<age>\d{2})', 'djkwif728372')
print(ret.group('name'))   # if
print(ret.group('age'))    # 72


ret = re.findall('www.(\w+).com', 'www.baidu.com')
print(ret)   # ['baidu']  这是因为分组权限较高,只打印分组内的内容。如果想打印全部内容在括号内加上 ?:

ret = re.findall('www.(?:\w+).com', 'www.baidu.com')
print(ret)   # ['www.baidu.com']
View Code

 

posted on 2019-01-16 23:52  mlllily  阅读(135)  评论(0编辑  收藏  举报