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. 【Error】Maven Dependency 下载失败问题

    原文 前言 在使用Maven私服Sonatype Nexus的时候,经常会出现依赖包找不到的问题. 此时通过浏览器去私服页面查看,发现依赖包坐标是存在的,对应的文件(比如jar文件). 或者私服上面也 ...

  2. PowerMock学习(三)之Mock局部变量

    编写powermock用例步骤: 类上面先写这两个注解@RunWith(PowerMockRunner.class).@PrepareForTest(StudentService.class) 先模拟 ...

  3. hdu 2255 奔小康赚大钱 (KM)

    奔小康赚大钱Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. nyoj 70-阶乘因式分解(二)(数学)

    70-阶乘因式分解(二) 内存限制:64MB 时间限制:3000ms 特判: No 通过数:7 提交数:7 难度:3 题目描述: 给定两个数n,m,其中m是一个素数. 将n(0<=n<=2 ...

  5. nyoj 833-取石子(七) (摆成一圈,取相邻)

    833-取石子(七) 内存限制:64MB 时间限制:1000ms 特判: No 通过数:16 提交数:32 难度:1 题目描述: Yougth和Hrdv玩一个游戏,拿出n个石子摆成一圈,Yougth和 ...

  6. 天啦!竟然从来没有人讲过 SpringBoot 支持配置如此平滑的迁移

    SpringBoot 是原生支持配置迁移的,但是官方文档没有看到这方面描述,在源码中才看到此模块,spring-boot-properties-migrator,幸亏我没有跳过.看到这篇文章的各位,可 ...

  7. Mac usr/bin 目录 权限问题

    Mac进行 usr/bin 目录下修改权限问题,operation not permitted 一般情况下我们在使用mac系统过程中下载一些文件.新建一些项目之后,这些文件都会默认是只读状态,这时我们 ...

  8. PHP中的服务容器与依赖注入的思想

    依赖注入 当A类需要依赖于B类,也就是说需要在A类中实例化B类的对象来使用时候,如果B类中的功能发生改变,也会导致A类中使用B类的地方也要跟着修改,导致A类与B类高耦合.这个时候解决方式是,A类应该去 ...

  9. cenos7搭建gitlab

    git.github和gitlab的区别 git:是一种版本控制系统,是一个命令,是一种工具 gitlib:是基于实现功能的开发库 github:是一个基于git实现的在线代码仓库软件 gitlib可 ...

  10. Glibc编译报错:*** LD_LIBRARY_PATH shouldn't contain the current directory when*** building glibc. Please change the environment variable

    执行glibc编译出错如下图 [root@localhost tmpdir]# ../configure --prefix=/usr/loacl/glibc2.9 --disable-profile ...