time 模块

time 模块提供了三种不同类型的时间, 三种不同类型的时间可以相互转换

时间戳形式

print(time.time())  # 1569668018.1848686

格式化时间

print(time.strftime('%Y-%m-%d %X'))  # 2019-01-28 18:55:25

结构化时间

print(time.localtime())  # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=18, tm_min=57, tm_sec=9, tm_wday=5, tm_yday=271, tm_isdst=0)

time.time()

获取当前时间的时间戳, 既表示从1970年1月1日00:00:00开始按秒计算的偏移量。

time.sleep()

推迟指定的时间运行下面的代码, 单位为秒

datetime 模块

datetime模块可以进行时间的加减

import datetime

# 获取当前时间
now = datetime.datetime.now()
print(now) # 2019-01-20 19:05:59.401263 # 当前时间加三天
print(now + datetime.timedelta(3)) # 2019-01-23 19:05:59.401263 # 当前时间加三周
print(now + date.time.timedelta(weeks=3) # 当前时间加三小时
print(now + datetime.timedelta(hours=3)) # 替换时间
print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0)) # 1949-10-01 10:01:00

random 模块

import random

# 0-1随机数
print(random.random()) # 0.37486363608725715 # 随机整数(包含两头)
print(random.randint(1,3)) # 1 # 打乱
lis = [1, 2, 3]
random.shuffle(lis)
print(lis) # [1, 3, 2] # 随机选择一个
print(random.choice(lis)) # 1 # 让括号内放入不会变化的数据如2, 则每次打印结果相同; 放入一直变化的数据如time.time()则每次打印结果不同
random.seed(2)
print(random.random) # 随机选择样本
print(random.sample(['a', 'b', 'c'],2) # ['b', 'c']

hashlib 模块 和 hmac 模块

对字符加密: 字符---哈希算法---> 一串hash值

特点:

  1. 只要传入内容一样, 得到的hash值就一样
  2. 不能由hash值反解成内容
  3. 只要hash算法不变, 得到的hash值长度是固定的
import hashlib

m = hashlib.md5()
print(m)
m.update(b'password')
print(m.hexdigest()) # 5f4dcc3b5aa765d61d8327deb882cf99

撞库破解hash算法加密

# 已知密码可能为下面的一个

real_pwd_hash = '5f4dcc3b5aa765d61d8327deb882cf99'

pwd_list = [
'password',
'password123',
'123passsword'
] find = False
for pwd in pwd_list: m = hashlib.md5()
m.update(pwd.encoding('utf-8'))
pwd_hash = m.hexdigest() if pwd_hash == real_pwd_hash:
continue
print(f'用户密码为: {pwd}')
find = True if not find:
print('未发现密码') # 用户密码为: password

hmac 模块可以对我们处理过的内容再次加密, 既相当于密匙, 可以防止被装库

typing 模块

与函数联用, 控制函数参数的数据类型, 提供了基础数据类型之外的数据类型


from typing import Iterable, Iterator, Generator def func(x: int, lis:Iterable) -> list:

requests 模块

模拟浏览器对url发送请求, 拿到数据

import requests

response=requests.get(r'https://baidu.com')
data = response.text
print(data)

re 模块

正则表达式: 去字符串中找到符合某种特点的字符串

元字符

import re

s = 'abcdabc'

# ^ : 以...开头
res = re.findall('^ab', s)
print(res) # $ : 以...结尾
res = re.findall('bc$',s)
print(res) # . : 任意字符
res = re.findall('abc.',s)
print(res) # \d : 数字
s = 'abc123def'
res = re.findall('\d', s)
print(res) # \w : 非空, 数字字母下划线
s = 'abc_123 456 def'
res = re.findall('\w', s)
print(res) # \s : 空, 空格,\t和\n
s = 'abc_123 456 def'
res = re.findall('\s', s)
print(res) # + : 其前面的一个字符至少有一个
s = 'abcddd abcd abc'
res = re.findall('abcd+', s)
print(res) # ? : 表示前面的一个字符至少为0个
s = 'abcddd abcd abc'
res = re.findall('abcd?', s)
print(res) # [] : 中括号内元素的都可以
s = 'abc bbc cbc dbc'
print(re.findall('[abc]bc', s)) # [^] : 中括号内元素的都不可以
s = 'abc bbc cbc dbc'
print(re.findall('[^abc]bc', s)) # | : 或
s = 'abc bbc cbc'
print(re.findall('abc|bbc', s)) # {2}:前面的一个字符有2个 s = 'abccabc abccc'
print(re.findall('abc{2}', s)) # {1,2}:前面的一个字符有1个或者2个
s = 'abccabc abccc'
print(re.findall('abc{1,2}', s))

贪婪匹配

# 贪婪匹配: 默认返回符合条件的最大长度的字符
# . 表示任意字符, * 表其前面的字符有0个或者无数个
s = 'abcdzeeeeeeeeeeeeeez'
res = re.findall('a.*z', s)
print(res) # ['abcdzeeeeeeeeeeeeeez'] # 非贪婪匹配: 返回符合条件的最小长度字符
# 使用问号 ? 即可进入非贪婪匹配
s = 'abcdzeeeeeeeeeeeeeez'
res = re.findall('a.*?z', s)
print(res) # ['abcdz']

match() 和 search()

match()函数 与 search()函数基本是一样的功能,不一样的就是match()匹配字符串开始位置的一个符合规则的字符串,search()是在字符串全局匹配第一个合规则的字符串

# match: 从字符串头部开始寻找, 找不到就报错
s = 'abc abcd abc'
res = re.match('abcd', s)
print(res.group()) # 报错 # search: 从字符串全局寻找
s = 'abc abcd abc'
res = re.search('abcd', s)
print(res.group()) # abcd

re.S

可以让. 匹配换行符\n

s = '''abc
abcabc*abc
''' print(re.findall('abc.abc', s)) # ['abc*abc']
print(re.findall('abc.abc', s, re.S)) # ['abc\nabc', 'abc*abc']

分组和有名分组

# 分组: 只要括号里面的
s = 'abc abcd abcd'
print(re.findall('a(.)c(d)', s)) # [('b', 'd'), ('b', 'd')] # 有名分组 ?P<>
s = 'abc abcd abcd'
print(re.search('a(?P<name1>.)c(?P<name2>d)', s).grounpdict()) # {'name1': 'b', 'name2': 'd'}

Python3 常用模块2的更多相关文章

  1. python3 常用模块详解

    这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...

  2. python3 常用模块

    一.time与datetime模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们 ...

  3. Python3常用模块的安装

    1.mysql驱动:mysql-connector-python 1.安装 $ pip3 install mysql-connector-python --allow-external mysql-c ...

  4. Python3 常用模块3

    目录 numpy模块 创建numpy数组 numpy数组的属性和用法 matplotlib模块 条形图 直方图 折线图 散点图 + 直线图 pandas模块 numpy模块 numpy模块可以用来做数 ...

  5. Python3 常用模块1

    目录 os模块 对文件夹操作 对文件进行操作 sys模块 json 和pickle模块 logging模块 日志等级 longging模块的四大组件 自定义配置 os模块 通过os模块我们可以与操作系 ...

  6. Python3基础(5)常用模块:time、datetime、random、os、sys、shutil、shelve、xml处理、ConfigParser、hashlib、re

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  7. Python3基础笔记--常用模块

    目录: 参考博客:Python 之路 Day5 - 常用模块学习 Py西游攻关之模块 一.time模块 二.random模块 三.os模块 四.sys模块 五.hashlib模块 六.logging模 ...

  8. day--6_python常用模块

    常用模块: time和datetime shutil模块 radom string shelve模块 xml处理 configparser处理 hashlib subprocess logging模块 ...

  9. python基础之常用模块以及格式化输出

    模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

随机推荐

  1. gitbook的插件配置

    原生的gitbook样式比较单一,美观度和功能欠佳,可通过相关插件进行拓展. 插件地址:https://plugins.gitbook.com/ 主目录下新建book.json: { "au ...

  2. nyoj 58-最少步数 (BFS)

    58-最少步数 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:17 submit:22 题目描述: 这有一个迷宫,有0~8行和0~8列: 1,1,1 ...

  3. 【SpringBoot | Redis】SpringBoot整合Redis

    SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...

  4. ReactJS中的自定义组件

    可控自定义组件: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  5. 部署k8s集群监控Heapster

    git clone https://github.com/kubernetes/heapster.gitkubectl apply -f heapster/deploy/kube-config/inf ...

  6. Dubbo面试八连问,这些你都能答上来吗?

    Dubbo是什么? Dubbo能做什么? Dubbo内置了哪几种服务容器? Dubbo 核心的配置有哪些? Dubbo有哪几种集群容错方案,默认是哪种? Dubbo有哪几种负载均衡策略,默认是哪种? ...

  7. vue通过控制boolean值来决定是否添加class类名

    vue通过控制boolean值来决定是否添加class类名

  8. PostgreSQL的使用向导

    目录 数据库 创建数据库 进入数据库 查看版本 查看当前时间日期 简单的select 获得帮助命令 退出psql客户端 创建表 weather和cities表的创建 删除表 插入数据 数据库导出成cs ...

  9. MySQL(学生表、教师表、课程表、成绩表)多表查询

    1.表架构 student(sid,sname,sage,ssex) 学生表 course(cid,cname,tid) 课程表 sC(sid,cid,score) 成绩表 teacher(tid,t ...

  10. 11-kubernetes RBAC 及授权

    目录 RBAC role 和 clusterrole rolebinding 和 clusterrolebinding 公共权限 clusterrole user 创建测试 创建role案例 创建 r ...