昨日内容

os模块

与操作用系统交互

sys模块

与python解释器交互

json模块

跨平台数据交互,json串

pickle模块

存储python所有类型的数据,为了python文件和python文件的交互

logging模块

日志记录

今日内容

  1. 包就是模块,拿来导入用的
  2. 包是含有__init__的文件夹,导包就是导入__init__
  3. 包一定被当作模块文件导入,模块文件的搜索路径以执行文件路径为准

相对导入

.代表当前被导入文件所在的文件夹

..代表当前被导入文件所在的文件夹的上一级

...代表当前被导入文件所在的文件夹的上一级的上一级

# aaa/.py

from .m1 import func1
from .m2 import func2

绝对导入

# aaa/.py

from aaa.m1 import func1
from aaa.m2 import func2

time模块

提供三种不同类型的时间,且可以互相转换

sleep

time.sleep(3)  # 暂停3s

时间戳 time

从1970年1月1日00时开始

import time
print(time.time())  # 

格式化时间 strtime

print(time.strtime('%Y-%m-%d %H:%M:%S'))
print(time.strtime('%Y-%m-%d %X'))

结构化时间 localtime

print(time.localtime())  

结构化时间-->格式化时间

struct_time = time.localtime(3600*24*365)
print(time.strtime('%Y-%m-%d %H:%M:%S',struct_time))

格式化时间-->结构化时间

format_time = time.strtime('%Y-%m-%d %H:%M:%S')
print(time.strptime(format_time,'%Y-%m-%d %H:%M:%S'))

结构化时间-->时间戳

struct_time = time.localtime(3600*24*365)
print(time.mktime(struct_time))

时间戳-->结构化时间

time_stamp = time.time()
print(time.localtime(time_stamp))

datetime模块

时间的加减

  1. datetime.now() 当前时间
  2. timedelta(整形)
import datetime

now = datetime.datetime.now()
prtin(now)  # 2019-09-28 18:40:11.829581当前时间

# 默认加/减天数
print(now + datetime.timedelta(3))  # 2019-10-01 18:40:11.829581

# 加3周
print(now + datetime.timedelta(week = 3))  

# 加减3小时
print(now + datetime.timedelta(hours = 3))

# 替换时间
print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))

random模块

随机数

  1. random 随机数(<1)
  2. randint 指定范围随机数
  3. shuffle 打乱顺序
  4. choice 随机选择
  5. seed 只随机一次
  6. sample 指定随机抽取多少个
import random

# 0-1的随机数
print(random.random())

# [1,3]的随机数
print(random.randit(1,3))

# 打乱顺序
lt = [1,2,3]
random.shuffle(lt)
print(lt)

# 随机选择一个
print(random.chioce(lt))

# 只随机一次
random.seed(1) # 根据传入的数字进行类似梅森旋转算法得出随机数

import time
random.seed(time.time())  # 根据时间得出随机数,每一次都会一样

# 了解
print(random.sample([1,2,3,'a'],2)) # 在列表中随机抽取2个数

hashlib模块和hmac模块

hashlib模块

对字符加密,并且加上密钥

import hashlib

m = hashlib.md5()
m.update(b'hello')
print(m.hexdigest())  # 根据hello生成的密钥

固定的字符串生成的密钥相同

hmac模块

密钥(加密钥)

import hmac

m = hmac.new(b'wick')  # 加的密钥

m.update(b'hash123456')  # 本身密码生成的密钥

# 最终存储的密钥是用户输入的密码生成的密钥加上内置密钥,更加难以破解

typing模块

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

#导入可迭代对象,迭代器对象,生成器三种数据类型
from typing import Iterable,Iterator,Generator

def func(x:int, y: Iterable)->list:
    return[1,2,3,4]

func(10,'qwer')

requests模块

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

import requests

response = requests.get('http://www.baidu.com')  # 向百度网站发送请求,得到一个响应

data = response.text  # 将响应读取成文本

res = re.findall('正则匹配',data,re.S)  # 利用正则re筛选出想要的数据

re模块

在字符串中筛选出符合某种特点的字符串

元字符

  1. ^ 以...开头

    import re
    s = 'abc红花dabc'
    
    res = re.findall('^ab',s)
    print(res)  # ['ab'] 按照\n分隔找元素
  2. $ 以...结尾

    res = re.findall('bc$',s)
    print(res)  # ['bc'] 根据\n判断结尾 
  3. . 代表任意字符(不匹配换行符)

    res = re.findall('abc.',s)
    print(res)  #['abc红'] 
  4. \d 数字

    s = 'asd313ew'
    res = re.findall('\d',s)
    print(res)  # ['3','1','3']
  5. \w 数字/字母/下划线

    s = 'af 1*-@ f_'
    res = re.findall('\w',s)
    print(res)  # ['a','f','1','f','_']
  6. \s 空格,\t,\n

    s = 'sdfa 324_sa#$@'
    res =re.findall('\s',s)
    print(res)  # [' ']
  7. \D 非数字

    s = 'a32s 3f '
    res =re.findall('\D',s)
    print(res)  # ['a','s',' ,'f',' ']
  8. \W 除了字母/数字/下划线

    s = 'sk-#l@d_23 42ljk'
    res = re.findall('\W', s)
    print(res)  # ['-','#','@',' ']
  9. \S 非空

    s = 'skld_23 42ljk'
    res = re.findall('\S', s)
    print(res)  # ['s', 'k', 'l', 'd', '_', '2', '3', '4', '2', 'l', 'j', 'k']
  10. + 前面一个字符至少有一个

    s = 'abcddddd abcd abc'
    print(re.findall('abcd+', s))  # ['abcddddd', 'abcd']
  11. ? 前面的一个字符0或者1个

    s = 'abcddddd abcd abc'
    print(re.findall('abcd?', s))  # ['abcd','abcd','abc']
  12. * 前面的一个字符至少0个

    s = 'abcdddddddddddddddddd abcd abc'
    print(re.findall('abcd*', s))  # ['abcdddddddddddddddddd', 'abcd', 'abc']
  13. [] 中括号内随便取一个都可以

    s = 'abc bbc cbc dbc'
    print(re.findall('[abc]bc', s))  # ['abc', 'bbc', 'cbc']
  14. [^]: 中括号的都不可以

    s = 'abc bbc cbc dbc'
    print(re.findall('[^abc]bc', s))  # ['dbc']
  15. |:或

    s = 'abc bbc dbc'
    print(re.findall('abc|bbc', s))  # ['abc','bbc']
  16. {2} 前面的字符2个

    s = 'abccabc abccc'
    print(re.findall('abc{2}', s))  # ['abcc', 'abcc']
  17. {1,2} 前面的字符1-2个

    s = 'abccabc abccc'
    print(re.findall('abc{1,2}', s))  # ['abcc', 'abc', 'abcc']

贪婪模式

.* 任意字符 + 至少0个

s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'
print(re.findall('a.*g', s))  # 包括'a'和最后一个'g'之间的所有字符,['abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg']

非贪婪模式

.*? 任意字符 + 至少0个 + 前一字符0或1个(控制进入非贪婪)

s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'
print(re.findall('a.*?g', s))  # 包括'a'和第一个'g'之间的所有字符['abcdefg']
  • bug

    s = 'abcdefg'
    print(re.findall('.*?', s))  # ['', 'a', '', 'b', '', 'c', '', 'd', '', 'e', '', 'f', '', 'g', '']

特殊构造 (了解)

  1. a(?=\d) a后面是数字,但是不要数字,且不消耗字符串内容

    s = 'a123 aaaa a234 abc'
    #    a1    aa
    #           aa
    #            aa a2    ab
    print(re.findall('a(?=\d)', s))  # ['a', 'a']
    print(re.findall('a(?=\w)', s))  # ['a', 'a', 'a', 'a', 'a', 'a']
    print(re.findall('a\w', s))  # ['a1', 'aa', 'aa', 'a2', 'ab']
    • 匹配邮箱

      s = '#@#@#@nickchen121@163.com$$$$////nick@qq.com$$#$#$[]]2287273393@162.com@$2423423lksdlfj#'
      # \w(字母/数字/下划线)+(0-无穷个)@ \w(字母/数字/下划线)+(0-无穷个).com
      print(re.findall('\w+@\w+.com', s))

函数

  1. compile 编译

    把正则匹配编译成一个变量

    s = 'abcd abcddd abc'
    
    # 邮箱匹配规则
    email_pattern = re.compile('\w+@\w+.com')
    
    # 手机号码匹配规则
    phone_patter = re.compile('\d{13}')
    print(re.findall(email_pattern, s))
    
    print(re.findall('abcd*', s))  # res = re.compile('abcd*')
  2. match 匹配

    只在字符串开头查找,找不到就报错

    s = 'ab abcddd abc'
    res = re.match('abc*', s)  # 对象
    print(res.group())  # ab
    
    res = re.match('abcd*', s)
    print(res.group())  # 报错
  3. search 查找

    从字符串中查找,找到就不找了

    s = 'ab abcddd abc'
    res = re.search('abcd*', s)
    print(res.group())  # abcddd
  4. split 切割

    s = 'ab23423abcddd234234abcasdfjl'
    print(re.split('\d+', s))  # ['ab', 'abcddd', 'abcasdfjl']
  5. sub 替换

    s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
    print(re.sub('\d+', ' ', s))  # ab abcddd abcasdfjlasjdk l lk j kl kl k j kl j lkj
    
  6. subn 替换,替换次数

    s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
    print(re.subn('\d+', ' ', s))  # ('ab abcddd abcasdfjlasjdk l lk j kl kl k j kl j lkj', 12)

修饰符

re.S 全局搜索(匹配换行符)

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 abcdd'
print(re.findall('a(.)c(d)', s))  # [('b', 'd'), ('b', 'd')]

有名分组 (了解)

a(?Pd)

s = 'abc abcd abcdd'
print(re.search('a(?P<name>.)c(?P<name2>d)', s).groupdict())  # {'name': 'b', 'name2': 'd'}

超高级用法

'c(?P\d+)a', '\g'

s = 'abc123abc123'  # c123a
print(re.sub('c(\d+)a', ' ', s))  # ab bc123
print(re.sub('c(?P<name1>\d+)a', '\g<name1>', s))  # \g<name1>这个东西不能替换掉  ab123bc123
# 以下必须得记住

# .*?
# 贪婪和非贪婪
# findall
# re.S
# match和sarch的区别
# 分组
# 有名分组:给分组加名字

# 哪些做了解

# 杂七杂八的元字符
# 特殊构造元字符
# 特殊修饰符

包+time+datetime+random+hashlibhmac+typing+requests+re模块(day17整理)的更多相关文章

  1. (常用)time,datetime,random,shutil(zipfile,tarfile),sys模块

    a.time模块import time 时间分为三种形式1.时间戳 (时间秒数的表达形式, 从1970年开始)print(time.time())start_time=time.time()time. ...

  2. python time、datetime、random、os、sys模块

    一.模块1.定义模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)包:用来从逻辑上组织 ...

  3. python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml

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

  4. Day13 Python基础之time/datetime/random模块一(十一)

    time模块 import time print(help(time)) time.time() #return current time in seconds since the Epoch as ...

  5. python笔记-1(import导入、time/datetime/random/os/sys模块)

    python笔记-6(import导入.time/datetime/random/os/sys模块)   一.了解模块导入的基本知识 此部分此处不展开细说import导入,仅写几个点目前的认知即可.其 ...

  6. Python-Windows下安装BeautifulSoup和requests第三方模块

    http://blog.csdn.net/yannanxiu/article/details/50432498 首先给出官网地址: 1.Request官网 2.BeautifulSoup官网 我下载的 ...

  7. [py]requests+json模块处理api数据,flask前台展示

    需要处理接口json数据,过滤字段,处理字段等. 一大波json数据来了 参考: https://stedolan.github.io/jq/tutorial/ https://api.github. ...

  8. python接口测试中安装whl格式的requests第三方模块

    下载 安装 requests第三方模块 下载:http://docs.python-requests.org/en/latest/user/install/#install 我下载是 https:// ...

  9. Python-hashlib、OS、Random、sys、zipfile模块

    # print(sys.version) #python 版本 # print(sys.path) # print(sys.platform) #当前什么系统 # print(sys.argv) #当 ...

随机推荐

  1. 规模、性能、弹性全面升级,让天下没有难用的 K8s

    作者 | 汤志敏 阿里云容器服务开发负责人 Kubernetes 是云原生时代的基础设施.云上的分布式操作系统. 9 月 26 日云栖大会容器专场,在<拐点已至,云原生引领数字化转型升级> ...

  2. meta标签设置(移动端)

    一.首先出结论:移动端meta标签一般设置为: <meta content="width=device-width,initial-scale=1.0,maxinmum-scale=1 ...

  3. 知名大厂如何搭建大数据平台&架构

    今天我们来看一下淘宝.美团和滴滴的大数据平台,一方面进一步学习大厂大数据平台的架构,另一方面也学习大厂的工程师如何画架构图.通过大厂的这些架构图,你就会发现,不但这些知名大厂的大数据平台设计方案大同小 ...

  4. Maven报错: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp

    郁闷了两天,创建maven项目时,eclipse报错:Could not resolve artifact org.apache.maven.archetypes:maven-archetype-we ...

  5. javascript中数组常用的方法和属性

    前言 在javascript中,数组是一种非常重要的数据类型,我们时常会和它打交道,最近在开发项目中频繁的使用到数组,但是自己对数组的众多方法已经是非常模糊了,为了方便自己以后能够更好的使用数组中的属 ...

  6. 阿里云服务器CentOS6.9 tomcat配置https安全访问

    应用场景 上线微信小程序的时候,域名要求https安全格式,否则获取数据异常. 第一步.SSL证书获取 获取SSL证书方式很多种,包括网页生成.工具生成等,这里我使用阿里云平台获取免费ssl证书的方法 ...

  7. centos 下安装 Let’s Encrypt 永久免费 SSL 证书

    功能 https证书,免费版,每三个月续签一次,可以用过脚本自动续签 安装 ssh登录到域名配置所在的主机(nginx,apache等) 安装git yum -y install git 输入 git ...

  8. C++——指针

    目录 一.地址和指针 1.1内存 1.2针和指针变量 二.指针变量 2.1始化 2.2赋值 2.3指针类型算术运算 2.4指针类型关系运算 2.5指向指针的指针 三.指针与数组 3.1指针运算 3.2 ...

  9. python爬虫——京东评论、jieba分词、wordcloud词云统计

    接上一章,动态页面抓取——抓取京东评论区内容. url=‘https://club.jd.com/comment/productPageComments.action?callback=fetchJS ...

  10. JavaScript 面向对象编程 · 理解对象

    前言:      在我们深入 面向对象编程之前 ,让我们先理解一下Javascript的 对象(Object),我们可以把ECMAScript对象想象成散列表,其值无非就是一组名值对,其中值可以是数据 ...