Python内置模块详解与实战应用
正则表达式中的转义处理
在原生正则中,反斜杠用于取消字符的特殊含义,每个反斜杠仅能转义一个字符。而在Python中推荐使用原始字符串(r前缀)来避免转义问题,也可结合反斜杠使用。
re模块核心功能
import re
# 查找所有匹配项
result = re.findall(r'\d+', 'abc123def456')
print(result) # ['123', '456']
# 检索首个匹配项
match_obj = re.search(r'^[A-Z]', 'Apple')
if match_obj:
print(match_obj.group()) # Apple
# 从头开始匹配
start_match = re.match(r'Hello', 'Hello World')
if start_match:
print(start_match.group()) # Hello
# 分割字符串
parts = re.split(r'[,\s]+', 'a,b c d,e')
print(parts) # ['a', 'b', 'c', 'd', 'e']
# 替换文本内容
replaced = re.sub(r'\d+', '数字', '第1个和第2个')
print(replaced) # 第数字个和第数字个
# 带替换次数限制的替换
limited_replace = re.sub(r'\d+', '数字', '1+2=3', count=1)
print(limited_replace) # 数字+2=3
# 编译正则以提高效率
pattern = re.compile(r'\d{3}-\d{4}')
phone_match = pattern.findall('电话:123-4567')
print(phone_match) # ['123-4567']
# 迭代式匹配
iter_matches = re.finditer(r'\w+', 'hello world')
for match in iter_matches:
print(match.group(), match.span()) # hello (0, 5), world (6, 11)
# 分组提取
id_card = '110105199812067023'
info = re.search(r'^(\d{6})(\d{8})(\d{3}[0-9X])$', id_card)
if info:
print(info.group(1)) # 110105
print(info.group(2)) # 19981206
print(info.group(3)) # 7023
网页数据抓取实战
import re
with open('branch_data.html', 'r', encoding='utf-8') as file:
content = file.read()
# 提取公司名称、地址、邮箱、电话
names = re.findall(r'<h2>(.*?)</h2>', content)
addresses = re.findall(r'<p class="mapIco">(.*?)</p>', content)
emails = re.findall(r'<p class="mailIco">(.*?)</p>', content)
phones = re.findall(r'<p class="telIco">(.*?)</p>', content)
# 组合信息并输出
for name, addr, email, phone in zip(names, addresses, emails, phones):
print(f"""
公司名称:{name}
地址:{addr}
邮箱:{email}
电话:{phone}
""")
collections模块高级数据结构
from collections import namedtuple, deque, OrderedDict, defaultdict, Counter
# 1. 命名元组:增强可读性
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # 10 20
# 2. 双端队列:支持两端插入删除
dq = deque([1, 2, 3])
dq.appendleft(0) # [0, 1, 2, 3]
dq.pop() # [0, 1, 2]
# 3. 有序字典:保持插入顺序
ordered = OrderedDict()
ordered['first'] = 1
ordered['second'] = 2
print(ordered) # OrderedDict([('first', 1), ('second', 2)])
# 4. 默认字典:自动初始化默认值
data = [10, 20, 30, 40, 50]
grouped = defaultdict(list)
for val in data:
if val > 30:
grouped['high'].append(val)
else:
grouped['low'].append(val)
print(grouped) # {'low': [10, 20, 30], 'high': [40, 50]}
# 5. 计数器:统计元素频率
text = "mississippi"
counter = Counter(text)
print(counter) # Counter({'i': 4, 's': 4, 'p': 2, 'm': 1})
queue模块:线程安全队列
import queue
q = queue.Queue(maxsize=5)
q.put('item1')
q.put('item2')
print(q.get()) # item1
print(q.get()) # item2
# 无数据时阻塞等待
# q.get() # 程序在此暂停,直到有新数据加入
time模块:时间操作
import time
# 休眠指定秒数
time.sleep(2)
# 获取时间戳
timestamp = time.time()
print(timestamp)
# 格式化时间
formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(formatted_time)
# 转换为结构化时间
local_time = time.localtime()
utc_time = time.gmtime()
datetime模块:日期与时间处理
import datetime
now = datetime.datetime.now()
print(now.date()) # 2024-04-05
print(now.time()) # 14:30:25.123456
print(now.year) # 2024
print(now.month) # 4
print(now.day) # 5
print(now.weekday()) # 4 (星期五,0=周一)
print(now.isoweekday()) # 5 (ISO标准,1=周一)
# 时间差计算
delta = datetime.timedelta(days=7, hours=3)
future = now + delta
past = now - delta
print(future) # 2024-04-12 17:30:25.123456