随机数
import random
1. random.random() 产生 [0, 1)的随机数
2. random.randrange(1, n) 产生 [1, n)的随机整数
时间
import time
1. 时间戳:time.time()
2. 时间对象(结构体):time.gmtime()
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=30, tm_hour=4, tm_min=39, tm_sec=30, tm_wday=5, tm_yday=334, tm_isdst=0)
3. 格式化时间字符串:print(time.strftime("%Y-%m-%d %H:%M:%S"))
4. 时间字符串转时间:print(time.strptime(“2020-01-01 01:01:03”, “%Y-%m-%d %H:%M:%S”))
5. 休眠0.5秒:time.sleep(0.5)
日志
import logging
1. 配置(只有在第一次调用该函数时会起作用,默认日志输出为sys.stderr)
日志格式:
LOG_FORMAT = “[ %(asctime)s %(levelname)s ] %(message)s”
追加到文件:(没文件则自动创建)
logging.basicConfig(filename="./my.log", level=logging.DEBUG, format=LOG_FORMAT)
输出到终端:
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT)
2. 输出
logging.debug(“This is a debug log.”)
logging.info(“This is a info log.”)
logging.warning(“This is a warning log.”)
logging.error(“This is a error log.”)
logging.critical(“This is a critical log.”)