python-模块(二)
re模块
在python中使用正则必须借助于模块,re是其中之一
re.findall()
1
2
3
4# 根据正则匹配所有符合条件的内容
res = re.findall('t', 'test adsa dcxzawqd ')
print(res) # ['t', 't'] 匹配到有元素时结果是一个列表,没有匹配到时是一个空列表re.search()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 根据正则匹配到一个符合条件的结束
res = re.search('d', 'test adsa dcxzawqd ')
print(res) # <_sre.SRE_Match object; span=(6, 7), match='d'>
print(res.group()) # d
# 返回的是一个结果对象,想要获取值需要通过 group()
res = re.search('o', 'test adsa dcxzawqd ')
print(res) # None
print(res.group()) # 当没有匹配到值时用 group() 取值会报错
# 可以使用判断是否取到值
if res:
print('res.group()')
else:
print('没匹配到值')re.match()
1
2
3
4
5
6
7
8
9
10
11
12# 根据正则从头开始匹配,开头匹配上了就停止匹配
res = re.match('a', 'abacad')
print(res) # <_sre.SRE_Match object; span=(0, 1), match='a'>
print(res.group()) # a
# 返回的也是一个结果对象,想获取值需要 group()
res = re.match('a', 'bbacad')
print(res) # None
print(res.group()) # 报错
# 当没有匹配到时也会报错re.split()
1
2
3
4
5# 先用 a 分割得到 '' 和 'bbcdd'
# 再用 b 分割得到 '' '' 和 'bcdd'
# 再用 b 分割得到 '' '' '' 'cdd'
res = re.split('[ab]','abbcdd')
print(res) # ['', '', '', 'cdd']re.sub()
1
2
3
4
5#类似于字符串类型的replace方法
res1 = re.sub('\d','H','eva3jason4yuan4',1) # 替换正则匹配到的内容
res2 = re.sub('\d','H','eva3jason4yuan4') # 不写默认替换所有
print(res1) # evaHjason4yuan4
print(res2) # evaHjasonHyuanHre.subn()
1
2
3
4
5# 返回元组 并提示替换了几处
res = re.subn('\d','H','eva3jason4yuan4',1)
print(res) # ('evaHjason4yuan4', 1)
res = re.subn('\d','H','eva3jason4yuan4')
print(res) # ('evaHjasonHyuanH', 3)re.compile()
1
2
3
4
5
6
7# 将正则表达式生成一个Pattern对象
regexp_obj = re.compile('\d+')
res1 = regexp_obj.search('absd213j1hjj213jk')
res2 = regexp_obj.match('123hhkj2h1j3123')
res3 = regexp_obj.findall('1213k1j2jhj21j3123hh')
print(res1, res2, res3) # <_sre.SRE_Match object; span=(4, 7), match='213'> <_sre.SRE_Match object; span=(0, 3), match='123'> ['1213', '1', '2', '21', '3123']
print(res1.group(), res2.group()) # 213 123re.finditer()
1
2
3# 将匹配到的内容存为一个迭代对象
res = re.finditer('\d+', 'ashdklah21h23kj12jk3klj112312121kl131')
print([i.group() for i in res]) # ['21', '23', '12', '3', '112312121', '131']分组优先展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 无名分组
# findall针对分组优先展示
res = re.findall("^[1-9]\d{14}(\d{2}[0-9x])?$",'110105199812067023')
print(res) # ['023']
# 取消分组优先展示
res1 = re.findall("^[1-9](?:\d{14})(?:\d{2}[0-9x])?$",'110105199812067023')
print(res1) # ['110105199812067023']
# 有名分组
res = re.search('^[1-9](?P<xxx>\d{14})(?P<ooo>\d{2}[0-9x])?$','110105199812067023')
print(res)
print(res.group()) # 110105199812067023
print(res.group(1)) # 10105199812067 无名分组的取值方式(索引取)
print(res.group('xxx')) # 10105199812067
print(res.group('ooo')) # 023通过正则获取网页信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25import re
import requests
res = requests.get('http://www.redbull.com.cn/about/branch')
if res.status_code == 200:
with open(r'index.html', 'wb') as f:
f.write(res.content)
with open('index.html', 'r', encoding='utf8') as f:
data = f.read()
title_list = re.findall('<h2>(.*?)</h2>', data)
address_list = re.findall("<p class='mapIco'>(.*?)</p>", data)
zip_code_list = re.findall("<p class='mailIco'>(.*?)</p>", data)
phone_list = re.findall("<p class='telIco'>(.*?)</p>", data)
res = zip(title_list, address_list, zip_code_list, phone_list)
# print(list(res))
for data in res:
print('''
公司名称: %s
公司地址: %s
公司邮编: %s
公司电话: %s
''' % (data[0], data[1], data[2], data[3]))
collections模块
该模块内部提供了一些高阶的数据类型
namedtuple()
1
2
3
4
5
6
7
8
9
10
11
12
13
14# 具名元组
from collections import namedtuple
point = namedtuple('坐标', ['x', 'y'])
res = point(11, 22)
print(res) # 坐标(x=11, y=22)
print(res.x) # 11
print(res.y) # 12
card = namedtuple('扑克', '花色 点数')
card1 = card('♠', 'A')
card2 = card('♥', 'K')
print(card1) # 扑克(花色='♠', 点数='A')
print(card2) # 扑克(花色='♥', 点数='K')deque()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23# 双端队列
# 队列模块
import queue # 内置队列模块:FIFO
# 初始化队列
# q = queue.Queue()
# 往队列中添加元素
q.put('first')
q.put('second')
q.put('third')
# 从队列中获取元素
print(q.get())
print(q.get())
print(q.get())
print(q.get()) # 值去没了就会原地等待
# deque()
from collections import deque
q = deque([11,22,33])
q.append(44) # 从右边添加
q.appendleft(55) # 从左边添加
print(q.pop()) # 从右边取值
print(q.popleft()) # 从做边取值OrderedDict()
1
2
3
4
5
6
7
8
9
10
11
12
13# 有序字典
# 无序的字典
normal_dict = dict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
print(normal_dict) # {'hobby': 'study', 'pwd': 123, 'name': 'jason'} 每次打印出来顺序都不一样
#
order_dict = OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
print(order_dict) # 打印结果顺序不变
OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
order_dict['xxx'] = 111
print(order_dict) # 添加的值在最后面
defaultdict()
1
2
3
4
5
6
7
8
9
10
11# 默认字典
from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
d = defaultdict(list)
for i in values:
if i > 60:
d['k1'].append(i)
else:
d['k2'].append(i)
print(d) # defaultdict(<class 'list'>, {'k2': [11, 22, 33, 44, 55], 'k1': [66, 77, 88, 99, 90]})Counter()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 统计字符出现的次数
res = 'abcdeabcdabcaba'
new_dict = {}
for i in res:
if i not in new_dict:
new_dict[i] = 1
else:
new_dict[i] += 1
print(new_dict) # {'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1}
# 使用Counter()
from collections import Counter
ret = Counter(res)
print(ret) # Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
time模块
时间的三种表现形式:
- 时间戳: 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
- 结构化时间: 元组(struct_time) 共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
- 格式化时间: 格式化的时间字符串(Format String): ‘1999-12-06’
1 | # 常用方法 |
python中时间日期格式化符号
1 | python中时间日期格式化符号: |
python中结构化时间
1 | import time |
几种格式之间的转换
datetime模块
1 | import datetime |