python内置模块之re模块
1、findall 、search、match
import re
# 根据正则匹配除所有符合条件的数据
re.findall('正则表达式','带匹配的文本')
res = re.findall('b','eva jason jackson')
print(res) # ['a', 'a', 'a'] 结果是一个列表(要么有元素 要么空列表)
# 根据正则匹配到一个符合条件的就结束
res = re.search('正则表达式','带匹配的文本')
res = re.search('a','eva jason jackson')
print(res) # 结果对象
print(res.group()) # 正在的结果
if res:
print(res.group())
else:
print('不好意思 没有找到')
"""如果没有符合条件的数据 那么search返回None 并且使用group会直接报错,报错可以通过if判断的方式解决"""
# 根据正则从头开始匹配(文本内容必须在开头匹配上)
res = re.match('a','abac')
print(res)
print(res.group())
if res:
print(res.group())
else:
print('不好意思 没有找到')
"""如果没有符合条件的数据 那么match返回None 并且使用group会直接报错,报错可以通过if判断的方式解决"""
2、split
import re
# 先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割
res = re.split('[ab]','abcd')
print(res) # ['', '', 'cd']
3、sub、subn
import re
# sub类似于字符串类型的replace方法
res = re.sub('\d','H','eva3jason4yuan4',1) # 替换正则匹配到的内容
res = re.sub('\d','H','eva3jason4yuan4') # 不写默认替换所有
print(res) # evaHjason4yuan4
# subn 返回元组 并提示替换了几处
res = re.subn('\d','H','eva3jason4yuan4',1)
print(res) # ('evaHjason4yuan4', 1)
res = re.subn('\d','H','eva3jason4yuan4')
print(res) # ('evaHjasonHyuanH', 3)
4、compile
# # 常用 类似于定义一个函数其他地方可以使用
import re
regexp_obj = re.compile('\d+')
res = regexp_obj.search('absd213j1hjj213jk')
res1 = regexp_obj.match('123hhkj2h1j3123')
res2 = regexp_obj.findall('1213k1j2jhj21j3123hh')
print(res,res1,res2)
5、finditer
res = re.finditer('\d+','ashdklah21h23kj12jk3klj112312121kl131')
print([i.group() for i in res])
6、分组
# search
res = re.search('^[1-9](\d{14})(\d{2}[0-9x])?$','110105199812067023')
print(res)
print(res.group()) # 110105199812067023
print(res.group(1)) # 10105199812067
print(res.group(2)) # 023
# findall
# 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)
# 有名分组
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
python内置模块之collections模块
该模块内部提供了一些高阶的数据类型
1、nametuple(具名元组)
格式
namedtuple('名称',[名字1,名字2,...])
namedtuple('名称','名字1 名字2 ...')
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) # 22
point = namedtuple('坐标', 'x y z')
res = point(11, 22, 33)
print(res) # 坐标(x=11, y=22, z=33)
print(res.x) # 11
print(res.y) # 22
print(res.z) # 33
2、队列
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()) # 值去没了就会原地等待
3、deque(双端队列)
from collections import deque
q = deque([11,22,33])
q.append(44) # 从右边添加
q.appendleft(55) # 从左边添加
print(q.pop()) # 从右边取值
print(q.popleft()) # 从做边取值
4、有序字典
# 普通字典转换
normal_dict = dict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
print(normal_dict)
{'hobby': 'study', 'pwd': 123, 'name': 'jason'}
# 使用模块
from collections import OrderedDict
order_dict = OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
print(order_dict)
OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
# 用模块添加一个元素(有序)
order_dict['xxx'] = 111
order_dict
OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study'), ('xxx', 111)])
# 用普通方式添加一个元素(无序)
normal_dict['yyy'] = 222
normal_dict
{'hobby': 'study', 'pwd': 123, 'yyy': 222, 'name': 'jason'}
5、默认值字典
from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in values:
if value>60:
my_dict['k1'].append(value)
else:
my_dict['k2'].append(value)
my_dict['k3'].append(111)
print(my_dict)
6、计数器
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)
from collections import Counter # 计数器
ret = Counter(res)
print(ret)
python内置模块之time模块
1、时间三种表现形式
时间戳(秒数)
结构化时间(一般给机器看)
格式化时间(一般给人看)
三种时间是可以相互转换的
2、时间戳
time.sleep() 原地阻塞指定的秒数
time.time() 获取时间戳时间
3、格式化时间
print(time.strftime('%Y-%m-%d')) # 2021-11-25
print(time.strftime('%Y-%m-%d %H:%M:%S')) # 2021-11-25 11:48:34
print(time.strftime('%Y-%m-%d %X')) # 2021-11-25 11:48:34
格式化时间字符
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
4、结构化时间(struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
print(time.localtime())
time.struct_time(
tm_year=2021,
tm_mon=11,
tm_mday=25,
tm_hour=11,
tm_min=51,
tm_sec=25,
tm_wday=3,
tm_yday=329,
tm_isdst=0)
5、三者之间转换关系
python内置模块之datetime模块
import datetime
print(datetime.date.today()) # 2021-11-25
print(datetime.datetime.today()) # 2021-11-25 17:00:00.383893
"""date年月日 datetime年月日时分秒 time时分秒(MySQL django后期可以)"""
单独打印
res = datetime.datetime.today()
print(res.year) # 2021
print(res.month) # 11
print(res.day) # 25
print(res.weekday()) # 获取星期(weekday星期是0-6) 0表示周一
print(res.isoweekday()) # 获取星期(weekday星期是1-7) 1表示周一
时间差
import datetime
ctime = datetime.datetime.today()
time_tel = datetime.timedelta(days=3)
print(ctime) # 2021-11-25 17:01:31.470390
# print(ctime - time_tel) # 2021-11-22 17:01:31.470390
print(ctime + time_tel) # 2021-11-28 17:02:00.575652
"""
日期对象 = 日期对象 +/- timedelta对象
timedelta对象 = 日期对象 +/- 日期对象
"""
ret = ctime + time_tel
print(ret - ctime) # 3 days, 0:00:00
print(ctime - ret) # -3 days, 0:00:00