datetime模块:提供日期和时间相关的功能。
import datetime
# 获取当前日期和时间
current_time = datetime.datetime.now()
# 格式化日期和时间
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
# 解析字符串为日期和时间
parsed_time = datetime.datetime.strptime("2022-01-01", "%Y-%m-%d")
7.calendar模块:提供日历相关的功能。
import calendar
# 获取指定月份的日历
cal = calendar.month(2022, 1)
print(cal)
8.logging模块:提供日志记录功能。
import logging
# 配置日志记录器
logging.basicConfig(filename='app.log', level=logging.INFO)
# 记录日志
logging.info('This is an info message')
9.argparse模块:提供命令行参数解析功能。
import argparse
# 创建解析器
parser = argparse.ArgumentParser()
# 添加参数
parser.add_argument('--name', help='Name argument')
# 解析命令行参数
args = parser.parse_args()
# 获取参数值
name = args.name
10.re模块:提供正则表达式匹配功能。
PYTHON复制import re
# 匹配字符串
pattern = r'\b\w+@\w+\.\w+\b'
email = re.match(pattern, 'example@example.com')
# 搜索字符串
matches = re.findall(pattern, 'Contact us at example@example.com or info@example.com')