Python基础语法文章导航:
目录
前言
本篇主要记录时间处理和正则表达式一些知识点的笔记。
一.时间处理
-
UTC/GMT:世界时间
-
本地时间:本地时区的时间。
Python中关于时间处理的模块有两个,分别是time和datetime。
1.time
python
import time
# 获取当前时间戳(自1970-1-1 00:00)
v1 = time.time()
print(v1)
# 时区
v2 = time.timezone
# 停止n秒,再执行后续的代码。
time.sleep(5)
2.datetime
在平时开发过程中的时间一般是以为如下三种格式存在:
python
from datetime import datetime, timezone, timedelta
v1 = datetime.now() # 当前本地时间
print(v1)
tz = timezone(timedelta(hours=7)) # 当前东7区时间
v2 = datetime.now(tz)
print(v2)
v3 = datetime.utcnow() # 当前UTC时间
print(v3)
# 2024-08-28 19:32:52.483083
# 2024-08-28 18:32:52.483083+07:00
# 2024-08-28 11:32:52.483083
python
from datetime import datetime, timedelta
v1 = datetime.now()
print(v1)
# 时间的加减
v2 = v1 + timedelta(days=140, minutes=5)
print(v2)
# datetime类型 + timedelta类型
# 2024-08-28 19:39:37.525485
# 2025-01-15 19:44:37.525485
python
from datetime import datetime, timezone, timedelta
v1 = datetime.now()
print(v1)
v2 = datetime.utcnow() # 当前UTC时间
print(v2)
# datetime之间相减,计算间隔时间(不能相加)
data = v1 - v2
print(data.days, data.seconds / 60 / 60, data.microseconds)
# datetime类型 - datetime类型
# datetime类型 比较 datetime类型
# 2024-08-28 19:44:01.714661
# 2024-08-28 11:44:01.714661
# 0 8.0 0
字符串
python
import datetime
# 字符串格式的时间 ---> 转换为datetime格式时间
text="2024-08-28"
v1=datetime.datetime.strptime(text,'%Y-%m-%d')# %Y 年,%m,月份,%d,天。
print(v1)
#2024-08-28 00:00:00
python
from datetime import datetime
# datetime格式 ----> 转换为字符串格式
v1 = datetime.now()
val = v1. strftime("%Y-%m-%d %H:%M:%S")
print(val)
#2024-08-28 19:58:52
时间戳
python
from datetime import datetime
import time
# 时间戳格式 --> 转换为datetime格式
ctime = time.time() # 11213245345.123
print(ctime)
v1 = datetime.fromtimestamp(ctime)
print(v1)
# 1724847941.0174716
# 2024-08-28 20:25:41.017472
python
from datetime import datetime
# datetime格式 ---> 转换为时间戳格式
v1 = datetime.now()
val = v1.timestamp()
print(val)
#1724847988.427188
二.正则表达式
当给你一大堆文本信息,让你提取其中的指定数据时,可以使用正则来实现。例如:提取文本中的邮箱和手机号。
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
phone_list = re.findall("1[3|5|8|9]\d{9}", text)
print(phone_list)
#['15131255789']
1.字符相关
(1)[zhangzuolin]匹配文本中的zhangzuolin
python
import re
text = "你好zhangzuolin,阿斯顿发zhangzuoliasd 阿士大夫能接受的zhangzuolinff"
data_list = re.findall("zhangzuolin", text)
print(data_list) # ['zhangzuolin', 'zhangzuolin'] 可用于计算字符串中某个字符出现的次数
(2)[abc]匹配a或b或c字符
python
import re
text = "你2b好wupeiqi,阿斯顿发awupeiqasd 阿士大夫a能接受的wffbbupqaceiqiff"
data_list = re.findall("[abc]", text)
print(data_list) # ['b', 'a', 'a', 'a', 'b', 'b', 'c']
python
import re
text = "你2b好wupeiqi,阿斯顿发awupeiqasd 阿士大夫a能接受的wffbbupqcceiqiff"
data_list = re.findall("q[abc]", text)
print(data_list) # ['qa', 'qc']
(3)[^abc]匹配除了abc之外的其他字符
python
import re
text = "你wffbbupceiqiff"
data_list = re.findall("[^abc]", text)
print(data_list) # ['你', 'w', 'f', 'f', 'u', 'p', 'e', 'i', 'q', 'i', 'f', 'f']
(4) [a-z]匹配a~z的任意字符([0-9]也可以)
python
import re
text = "alexrootrootadmin"
data_list = re.findall("t[a-z]", text)
print(data_list) # ['tr', 'ta']
(5) . 代指除换行符以外的任意字符
python
import re
text = "alexraotrootadmin"
data_list = re.findall("r.o", text)
print(data_list) # ['rao', 'roo']
python
import re
text = "alexraotrootadmin"
data_list = re.findall("r.+o", text) # 贪婪匹配
print(data_list) # ['raotroo']
python
import re
text = "alexraotrootadmin"
data_list = re.findall("r.+?o", text) # 非贪婪匹配
print(data_list) # ['rao']
(6)\w
代指字母或数字或下划线(汉字)
python
import re
text = "北京武沛alex齐北 京武沛alex齐"
data_list = re.findall("武\w+x", text)
print(data_list) # ['武沛alex', '武沛alex']
(7)\d代指数字
python
import re
text = "root-ad32min-add3-admd1in"
data_list = re.findall("d\d", text)
print(data_list) # ['d3', 'd3', 'd1']
python
import re
text = "root-ad32min-add3-admd1in"
data_list = re.findall("d\d+", text)
print(data_list) # ['d32', 'd3', 'd1']
(8) \s
代指任意的空白符,包括空格、制表符等。
python
import re
text = "root admin add admin"
data_list = re.findall("a\w+\s\w+", text)
print(data_list) # ['admin add']
2.数量相关
(1)*重复0次或更多次
python
import re
text = "他是大B个,确实是个大2B。"
data_list = re.findall("大2*B", text)
print(data_list) # ['大B', '大2B']
(2)+重复1次或更多次
python
import re
text = "他是大B个,确实是个大2B,大3B,大66666B。"
data_list = re.findall("大\d+B", text)
print(data_list) # ['大2B', '大3B', '大66666B']
(3)?重复0次或1次
python
import re
text = "他是大B个,确实是个大2B,大3B,大66666B。"
data_list = re.findall("大\d?B", text)
print(data_list) # ['大B', '大2B', '大3B']
(4){n} 重复n次
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("151312\d{5}", text)
print(data_list) # ['15131255789']
(5){n,}重复n次或更多次
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("\d{9,}", text)
print(data_list) # ['442662578', '15131255789']
(6){n,m}重复n到m次
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("\d{10,15}", text)
print(data_list) # ['15131255789']
3.括号(分组)
(1)提取数据区域
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("15131(2\d{5})", text)
print(data_list) # ['255789']
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来15131266666呀"
data_list = re.findall("15(13)1(2\d{5})", text)
print(data_list) # [ ('13', '255789') ]
python
import re
text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("(15131(2\d{5}))", text)
print(data_list) # [('15131255789', '255789')]
(2)获取指定区域 + 或条件
python
import re
text = "楼主15131root太牛15131alex逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("15131(2\d{5}|r\w+太)", text)
print(data_list) # ['root太', '255789']
python
import re
text = "楼主15131root太牛15131alex逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀"
data_list = re.findall("(15131(2\d{5}|r\w+太))", text)
print(data_list) # [('15131root太', 'root太'), ('15131255789', '255789')]
4.起始和结束
如果要求用户输入的内容必须是指定的内容开头和结尾,比就需要用到如下两个字符。
-
^
开始 -
$
结束
python
import re
text = "啊442662578@qq.com我靠"
email_list = re.findall("^\w+@\w+.\w+$", text, re.ASCII)
print(email_list) # []
python
import re
text = "442662578@qq.com"
email_list = re.findall("^\w+@\w+.\w+$", text, re.ASCII)
print(email_list) # ['442662578@qq.com']
这种场景常用于:
python
import re
text = input("请输入邮箱:")
email = re.findall("^\w+@\w+.\w+$", text, re.ASCII)
if not email:
print("邮箱格式错误")
else:
print(email)
5.特殊字符
由于正则表达式中 * . \ { } ( )
等都具有特殊的含义,所以如果想要在正则中匹配这种指定的字符,需要转义,例如:
python
import re
text = "我是你{5}爸爸"
data = re.findall("你{5}爸", text)
print(data) # []
python
import re
text = "我是你{5}爸爸"
data = re.findall("你\{5\}爸", text)
print(data)
6.re模块
python中提供了re模块,可以处理正则表达式并对文本进行处理。
(1)findall,获取匹配到的所有数据
python
import re
text = "dsf130429191912015219k13042919591219521Xkk"
data_list = re.findall("(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)", text)
print(data_list) # [('130429', '1919', '12', '01', '521', '9'), ('130429', '1959', '12', '19', '521', 'X')]
(2)match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None
python
import re
text = "大小逗2B最逗3B欢乐"
data = re.match("逗\dB", text)
print(data) # None
python
import re
text = "逗2B最逗3B欢乐"
data = re.match("逗\dB", text)
if data:
content = data.group() # "逗2B"
print(content)
(3)search,浏览整个字符串去匹配第一个,未匹配成功返回None
python
import re
text = "大小逗2B最逗3B欢乐"
data = re.search("逗\dB", text)
if data:
print(data.group()) # "逗2B"
(4)sub,替换匹配成功的位置
python
import re
text = "逗2B最逗3B欢乐"
data = re.sub("\dB", "沙雕", text)
print(data) # 逗沙雕最逗沙雕欢乐
python
import re
text = "逗2B最逗3B欢乐"
data = re.sub("\dB", "沙雕", text, 1)
print(data) # 逗沙雕最逗3B欢乐
(5)split,根据匹配成功的位置分割
python
import re
text = "逗2B最逗3B欢乐"
data = re.split("\dB", text)
print(data) # ['逗', '最逗', '欢乐']
python
import re
text = "逗2B最逗3B欢乐"
data = re.split("\dB", text, 1)
print(data) # ['逗', '最逗3B欢乐']
(6)finditer
python
import re
text = "逗2B最逗3B欢乐"
data = re.finditer("\dB", text)
for item in data:
print(item.group())
python
import re
text = "逗2B最逗3B欢乐"
data = re.finditer("(?P<xx>\dB)", text) # 命名分组
for item in data:
print(item.groupdict())
python
text = "dsf130429191912015219k13042919591219521Xkk"
data_list = re.finditer("\d{6}(?P<year>\d{4})(?P<month>\d{2})(?P<day>\d{2})\d{3}[\d|X]", text)
for item in data_list:
info_dict = item.groupdict()
print(info_dict)
总结
理解即可,当遇到对数据进行处理的情况时,能够正确使用正则表达式即可。