"""
p021:统计学生爱好的人数
"""
if __name__ == '__main__':
like_count = {}
with open("./p021_student_like.txt", encoding="utf-8") as fp:
for line in fp:
line = line[:-1]
sname, likes = line.split()
like_list = likes.split(",")
for like in like_list:
if like not in like_count:
like_count[like] = 0
like_count[like] += 1
print(like_count)
小张 篮球,羽毛球
小王 篮球,乒乓球
小李 篮球,台球
小赵 篮球,足球,台球
小马 乒乓球,台球
小钱 羽毛球,足球
小孙 乒乓球,台球
小强 羽毛球
"""
p022:获取当前的日期时间
"""
import datetime
if __name__ == '__main__':
curr_datetime = datetime.datetime.now()
print(curr_datetime, type(curr_datetime))
str_time = curr_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("str_time:", str_time)
print("year:", curr_datetime.year)
print("month:", curr_datetime.month)
print("day:", curr_datetime.day)
print("hour:", curr_datetime.hour)
print("minute:", curr_datetime.minute)
print("second:", curr_datetime.second)
"""
p023:计算自己活了多少天
"""
import datetime
if __name__ == '__main__':
birthday = '1997-12-21'
birthday_date = datetime.datetime.strptime(birthday, "%Y-%m-%d")
print(birthday_date)
current_datetime = datetime.datetime.now()
print(current_datetime)
minus_datetime = current_datetime - birthday_date
print(minus_datetime)
print("days:", minus_datetime.days)
"""
p024:计算任意日期7天前的日期
"""
import datetime
def get_diff_days(pdate, days):
pdate_obj = datetime.datetime.strptime(pdate, "%Y-%m-%d")
time_gap = datetime.timedelta(days=days)
pdate_result = pdate_obj - time_gap
return pdate_result.strftime("%Y-%m-%d")
if __name__ == '__main__':
pdate = "2022-01-16"
print(get_diff_days(pdate, 7))
"""
p025:获取开始到结束的所有日期
"""
import datetime
def get_date_range(begin_date, end_date):
date_list = []
while begin_date <= end_date:
date_list.append(begin_date)
begin_date_obj = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
days1_timedate = datetime.timedelta(days=1)
begin_date = (begin_date_obj + days1_timedate).strftime("%Y-%m-%d")
return date_list
if __name__ == '__main__':
begin_date = "2021-04-28"
end_date = "2021-05-03"
date_list = get_date_range(begin_date, end_date)
print(date_list)
"""
p026: unix时间戳转换为日期格式
"""
import datetime
if __name__ == '__main__':
unix_time = 1620747647
datetime_obj = datetime.datetime.fromtimestamp(unix_time)
datetime_str = datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
print(datetime_str)
"""
p027:正则表达式,验证字符串是不是日期
"""
import re
def date_is_right(date):
return re.match("\d{4}-\d{2}-\d{2}", date) is not None
date1 = "2022-01-16"
print(date_is_right(date1))
date2 = "202-01-16"
print(date_is_right(date2))
date3 = "2022/01-16"
print(date_is_right(date3))
date4 = "20220116"
print(date_is_right(date4))
date5 = "202a-01-16"
print(date_is_right(date5))
"""
p028:从一段文本中提取所有手机号码
"""
import re
if __name__ == '__main__':
content = """
青春23是什么,每个人都有12345678自己的见解。我们只是爱借523645着青2342春的名义再13953652564肆无忌惮一次,用时光绘22342画着未
来的颜色,用汗水打造13987456985着56456理想的殿堂,用岁月承载134569525365着曾经的梦想,再将他们一一打磨,雕刻成最美丽123456952
65478的模样。我们只是想14425这青葱的岁月,留下些2342回忆,这样的回忆,是能够让13777777777我们在多年之后笑着流泪,说,我不曾后悔。
"""
pattern = r"1[3-9]\d{9}"
# 第一位是1,第二位是3 - 9,总共11位数字
results = re.findall(pattern, content)
for item in results:
print(item)
"""
p029: 自动提取邮箱地址
"""
import re
if __name__ == '__main__':
content = """
大多数Windows文本文件使用ANSI、OEM或者Unicode编码。Windows
所指的ANSI编码通常是1字节的ISO-8859编码,不过对于像中文、日文、
朝鲜文这样的环境,需要使用2字节字符集14555110@qq.com。在过渡至Unicode前,Windows
一直用ANSI作a12545bcded.com为系统默认的编码。而OEM编码,也是通常所说的MS-DOS代码页,
是IBM为早期IBM个人电脑的文本abcded@qq.cn模式显示系统定义的。在全屏的MS-DOS程序中
同时使用了图形的python_abc@sina.comabc.com和按行绘制的字符。新版本的Windows可以使用UTF-16LE和
UTF-8之类的Unicode编码。
"""
# verbose,多行编写
# a-z A-Z 0-9 _ - (+至少一个符号)
# @
# a-z A-Z 0-9 (+至少一个符号)
# . (转义)
# a-z A-Z {2,4}(最多出现2-4个)
pattern = re.compile(r"""
[a-zA-Z0-9_-]+
@
[a-zA-Z0-9]+
\.
[a-zA-Z]{2,4}
""", re.VERBOSE
)
results = re.findall(pattern, content)
for email in results:
print(email)
"""
p030: 验证用户名密码是否合规
写一个函数,验证密码是否满足条件
1.长度位于6-20之间
2.必须包含至少1个小写字母
3.必须包含至少1个大写字母
4.必须包含至少1个数字
5.必须包含至少1个特殊字符
"""
import re
def check_password(password):
if not 6 <= len(password) <= 20:
return False, "密码必须在6-20之间"
if not re.findall(r"[a-z]", password):
return False, "必须包含至少1个小写字母"
if not re.findall(r"[A-Z]", password):
return False, "必须包含至少1个大写字母"
if not re.findall(r"[0-9]", password):
return False, "必须包含至少1个数字"
if not re.findall(r"[^a-zA-Z0-9]", password):
return False, "必须包含至少1个特殊字符"
return True, None
password1 = "zZ12345678@"
print(check_password(password1))
password2 = "Z12345678@"
print(check_password(password2))
password3 = "z12345678@"
print(check_password(password3))
password4 = "zZSDFSDF@"
print(check_password(password4))
password5 = "zZ12345678"
print(check_password(password5))
password5 = "zZ12@"
print(check_password(password5))