Python---模块与正则表达式

Python从入门到高手(内部资源-配学习资料)_哔哩哔哩_bilibili

# 加密算法 md5 sha1 sha256
# base64
import hashlib

msg = '于鹏中午一起吃饭去!'
md5 = hashlib.md5(msg.encode('utf-8'))
print(md5.hexdigest())

sha1 = hashlib.sha1(msg.encode('utf-8'))
print(sha1.hexdigest())

sha256 = hashlib.sha256(msg.encode('utf-8'))
print(sha256.hexdigest())

password = '123456'
list1 = []
sha256 = hashlib.sha256(password.encode('utf-8'))
list1.append(sha256.hexdigest())

pwd = input('Please input your code:')
sha256 = hashlib.sha256(pwd.encode('utf-8'))
pwd = sha256.hexdigest()
print(pwd)
print(list1)
for i in list1:
    if pwd == i:
        print('online')

---------------------------------------------------------------------------------------------------------

# random模块

import random

ran = random.random()  # 得到0-1的小数
ran = random.randrange(1, 10, 2)  # randrange(start,stop,step) 1-10 step=2--->1,3,5,7,9
ran = random.randint(1, 10)
list1 = ['renwen', 'lianting', 'ad', 'tt', 'rr']
ran = random.choice(list1)  # 随机选择列表的内容
print(ran)

card = ['红桃A', '方片K', '梅花8', '黑桃5']
result = random.shuffle(card)  # 打乱顺序
print(card)


# 验证码 大写字母与数字的组合
def func():
    code = ''
    for i in range(4):
        ran1 = str(random.randint(0, 9))
        ran2 = chr(random.randint(65, 90))
        ran3 = chr(random.randint(97, 122))
        r = random.choice([ran1, ran2, ran3])
        code += r
    return code


code1 = func()
print(code1)

-------------------------------------------------------------------------------------------------------

# qq = input('输入QQ号码')
# if len(qq) >= 5 and qq[0] != '0':
#     print('合法的')
# else:
#     print('不合法的')

import re

# msg = '扎娜热巴代斯佟丽娅'
# pattern = re.compile('佟丽娅')
# result = pattern.match(msg)  # 没有匹配,从头开始匹配
# print(result)
#
# # 使用正则re模块方法:match
# s = '扎娜热巴代斯佟丽娅'
# re.match('佟丽娅', s)  # 只要从开头进行匹配,如果匹配不成功则返回None
# print(result)
#
# result = re.search('佟丽娅', s)  # search进行正则字符串匹配方法,匹配的是整个字符串
# print(result)
# print(result.span())  # 返回位置
# print(result.group())  # 使用group提取到匹配的内容
# print(result.groups())

# # a2b h6k
# msg = 'abcd7v jkfd8hdf00'
# result = re.findall('[a-z][0-9][a-z]', msg)
# print(result)
#
# # qq号码验证5-11 开头不能是0
# qq = '14944689962'
# result = re.match('^[1-9][0-9]{4,10}$', qq)
# print(result)

# # 用户名可以是字母或者数字,不能是数字开头,用户名长度必须6位以上[0-9a-zA-Z]
# username = 'admin001'
# result = re.match('[a-zA-Z][0-9a-zA-Z]{5,}$', username)  # $表示一直匹配到结尾
# print(result)

username = '#$@admin001'
result = re.search('^[a-zA-Z][0-9a-zA-Z_]{5,}$', username)  # 加^表示'#$@admin001'的#¥@不规范
print(result)

# 用户名可以是字母或者数字_,不能是数字开头,用户名长度必须6位以上[0-9a-zA-Z]
username = 'admin001'
result = re.search('^[a-zA-Z]\w{5,}$', username)  # 加^表示'#$@admin001'的#¥@不规范
print(result)

msg = 'aa.py ab.txt bb.py kk.png uu.py apvb.txt'
result = re.findall(r'\w*\.py\b', msg)
print(result)
'''
总结:
. 任意字符除(\n)
^ 开头
$ 结尾
re.search()要结合^和$来使用
[] 范围 [abc][a-z][a-z*&$]

正则预定义:
\s  空白(空格)
\b  边界
\d  数字
\w  word  [0-9a-zA-Z]
大写反面 \S 非空格  \D 非数字
‘\w[0-9]’--->\w [0-9] 只能匹配一个字母

量词:
*  >=0
+  >=1
?  0,1


手机号码正则
re.match('1[35789]\d{9}$',phone)

{m}:固定m位
{m,} >=m
{m,n} phone>=m  phone<=n
'''

--------------------------------------------------------------------------------------

# 分组
# 匹配数字0-100数字
import re

# n = '500'
#
# result = re.match('[1-9]?\d', n)  # 前面的内容可以有也可以没有
# print(result)

# # 分组
# # 匹配数字0-100数字
# n = '1000'
# result = re.match(r'[1-9]?\d?$|100$', n)  # |或者
# print(result)
#
# # (word|word|word) 与 [abc]区别:[abc]表示一个字母
# # 验证输入的邮箱 163 126 qq
# email = '738473800@qq.com'
# result = re.match(r'\w{5,20}@(163|126|qq)\.(com|cn)', email)  # ()表示整体范围的或者
# print(result)

# phone = '15901018869'
# result = re.match(r'1\d{9}[0-35-689]$', phone)
# print(result.group())

# # 爬虫
# phone = '010-13456789'
# result = re.match(r'(\d{3}|\d{4})-(\d{8})$', phone)
# print(result)
# # 分别提取
# print(result.group())
# # ()表示分组  group(1)表示提取到第一组的内容  group(2)表示提取到第二组的内容
# print(result.group(1))
# print(result.group(2))

# #
# msg = '<html>abc</html>'
# msg1 = '<h1>hello</h1>'
# result = re.match(r'<[0-9a-zA-Z]+>(.+)</[0-9a-zA-Z]+>$', msg)
# print(result)
# print(result.group(1))

# # number
# msg = '<html>abc</html>'
# msg1 = '<h1>hello</h1>'
# result = re.match(r'<([0-9a-zA-Z]+)>(.+)</\1>$', msg)  # \1引用第一组匹配的内容
# print(result)
# print(result.group(1))
# print(result.group(2))

# msg = '<html><hl>abc</hl></html>'
# result = re.match(r'<([0-9a-zA-Z]+)><([0-9a-zA-Z]+)>(.+)</\2></\1>$', msg)
# print(result)
# print(result.group(1))
# print(result.group(2))

--------------------------------------------------------------------------------------------------------------

# time模块
# 1.时间戳
import time
import datetime

t = time.time()
print(t)
# time.sleep(3)
t1 = time.time()
print(t1)

# 将时间戳转化为字符串
s = time.ctime(t)
print(s)

t = time.localtime(t)  # 以元组的形式获取时间戳
print(t)
print(t.tm_hour)

# 将元组转化为时间戳
tt = time.mktime(t)
print(tt)

# 将元组转化为字符串
s = time.strftime('%Y-%m-%d %H:%M:%S')
print(s)
'''
    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
'''

# 将字符串转成元组的方式
r = time.strptime("2019/06/20", "%Y/%m/%d")
print(r)
print(time.localtime().tm_hour)

print(datetime.date.day)
d = datetime.date(2019, 6, 20)
print(datetime.date.ctime(d))

# datatime,timedelta
print(datetime.date.today())  # 2019-06-20
# datetime.datetime.now()------》得到当前的日期和时间
timedel = datetime.timedelta(hours=2)
print(timedel)
now = datetime.datetime.now()
result = now - timedel
print(result)
'''
time模块:
重点:
time()
sleep()
strftime('格式')  

datetime模块:
time时间
date日期
datetime日期时间 now()
timedelta时间差 timedelta(days='',weeks='',...)
'''

# 缓存:数据redis作为缓存 redis.set(key,value,时间差) 会话:session

----------------------------------------------------------------------------------------------------------------------

# chr
print(chr(65))  # Unicode码---》str
print(ord('A'))  # str---》Unicode码
print(ord('下'))  # 19979
print(chr(19979))

---------------------------------------------------------------------------------------------------------

1.正则表达式的定义
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符及这些特定字符的组合,组成一个‘规则字符串’,这个“规则字符串”用来表达对字符串的一种过滤逻辑。
1).长度
2).是否是0开头
正则表达式是对字符串(包括普通字符(例如,a到z之间的字母)和特殊字符(称为‘元字符’))操作的一种逻辑公式,就是用事先定义好的一些特定字符及这些特定字符的组合,组成
一个‘规则字符串’,这个‘规则字符串’用来表达对字符串的一种过滤逻辑。正则表达式是一种文本模式,模式描述在搜索文本时要匹配的一个或多个字符串。
正则表达式

2. 正则表达式的作用和特点
给定一个正则表达式和另一个字符串,我们可以达到如下目的:
1).给定的字符串是否符合正则表达式的过滤逻辑(称作‘匹配’)
2).可以通过正则表达式,从字符串中获取我们想要的特定部分

正则表达式的特点是:
1.灵活性、逻辑性或功能性非常强
2.可以迅速地用极简单的方式达到字符串的复杂控制
3.对于刚接触的人来说,比较晦涩难懂

python re模块:
    \A       Matches only at the start of the string.
    \Z       Matches only at the end of the string.
    \b       Matches the empty string, but only at the start or end of a word.
    \B       Matches the empty string, but not at the start or end of a word.
    \d       Matches any decimal digit; equivalent to the set [0-9] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode digits.
    \D       Matches any non-digit character; equivalent to [^\d].
    \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode whitespace characters.
    \S       Matches any non-whitespace character; equivalent to [^\s].
    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
             in bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the
             range of Unicode alphanumeric characters (letters plus digits
             plus underscore).
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
    \W       Matches the complement of \w.
    \\       Matches a literal backslash.


-------------------------------------------------------------------------------
# 第三方
# import pillow
import requests

response = requests.get('http://www.baidu.com/')
print(response.text)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值