day17

馋奶兔 提交于 2019-11-30 16:39:22

# 1. 包 == 模块, 包拿来导入用的
# 2.包是含有__init__.py的文件夹; 导包就是导入__init__
# 3.包一定是被当作模块文件导入,模块文件 m1.py/m2.py 的搜索路径以执行文件 包的介绍.py 路径为准
# 相对导入绝对导入: 只能在包中内部使用
# 包的作用:当模块内部函数过多,为了方便管理模块,把一个模块划分成多个模块,但是又不能改变导入方式,把多个
# 模块放入一个包(文件夹)内。未来导包就是导init

time模块

import time
'''
提供了三种不同类型的时间(时间戳)
三种不同类型的时间可以相互转换
'''

#时间戳形式
print(time.time())

#格式化时间
print(time.strftime('%Y-%m-%d %X'))

# 结构化时间
print(time.localtime())


#结构化时间——>格式化时间
struct_time = time.localtime(second)
print(time.strftime('%Y-%m-%d %X',struct_time))

#格式化时间——>结构化时间
format_time = time.strftime('%Y-%m-%d %X')
print(time.strptime(format_time,'%Y-%m-%d %X'))


#结构化时间——>时间戳
struct_time = time.localtime(second)
print(time.mktime(struct_time))

#时间戳——>结构化时间
time_stamp = time.time()
print(time.localtime(time_stamp))

datetime模块

import datetime
'''
时间的加减
'''

#当前时间
now = datetime.datetime.now()
print(now)

#默认是加减天,最大单位是week,都是默认形参
print(now + datetime.timedelta(day = 3)
print(now - datetime.timedelta(week = 3)
print(now + datetime.timedelta(hour = 3)
print(now - datetime.timedelta(second = 3)

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

random模块

import random
'''
随机数
'''

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

#[start, end]的随机数
print(random.randint(start,end))

#乱序
print(random.shuffle(lis))

#随机选一个
print(random.choice(lis))

#random.seed随机种子
#梅森旋转算法,默认随机,当有固定的种子数时,输出固定
random.seed(3)  
print(random.random())  

#随机组合

print(random.sample(lis, n))  #n表示取n个数组合

hashlib和hmac模块

hashlib

import hashlib
'''
对字符加密
'''
#叠加性
m = hashlib.md5()
m.update(b'say')
print(m.hexdigest())

hmac

import hmac

m = hmac.new(b'maerzi')
m.update(b'hash123456')  
print(m.hexdigest())

typing模块

import typing
'''
与函数联用
控制函数参数的数据类型
提供了基础数据类型之外的数据类型
'''
#只是给个提示
def func(x: int, lt: Iterable) -> list:
    return [1, 2, 3]

requests模块

import requests
'''
爬虫
模拟浏览器对url发送请求,拿到数据
url:一个特定的网址,永不重复
'''

response = requests.get(url = ''https://ishuo.cn'')
data = response.text
print(data)


#正则re
'''
从大的字符串中,挑选出具有某种形状特点的字符串
'''

#.*?表示所有类型的都要

re模块

import re
'''
取字符串找某种特点的字符串
'''

#re.findall(pattern, str)


#^以。。。开头
s = 'sd sdf'
res = re.findall('^sd',s)
print(res)  #['sd']


# $: 以..结尾
s = 'abcdabc'
res = re.findall('bc$', s)
print(res)  #['bc']

# .: 任意字符
s = 'abc红abc'
res = re.findall('abc.', s)
print(res)  #['abc红']

# \d: 数字
s = 'skld2342ljk'
res = re.findall('\d', s)
print(res)  #['2', '3', '4', '2']

# \w: 非空,数字字母下划线
s = 'skld_23 42ljk'
res = re.findall('\w', s)
print(res)  #['s', 'k', 'l', 'd', '_', '2', '3', '4', '2', 'l', 'j', 'k']

# \s:空,单个空格/\t/\n
s = 'skld_23  42ljk'
res = re.findall('\s', s)
print(res)  #[' ', ' ']

# \D: 非数字
s = 'skld2342ljk'
res = re.findall('\D', s)
print(res)  #['s', 'k', 'l', 'd', 'l', 'j', 'k']

# \W: 用于匹配所有与\w不匹配的字符
s = 'skl@d_23 42ljk'
res = re.findall('\W', s)
print(res)  #['@', ' ']

# \S:非空
s = 'skld_23 42ljk'
res = re.findall('\S', s)
print(res)  #['s', 'k', 'l', 'd', '_', '2', '3', '4', '2', 'l', 'j', 'k']

# +: 前面的一个字符至少1个
s = 'abcddddd abcd abc'
print(re.findall('abcd+', s))  #['abcddddd', 'abcd']

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

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

# []: 中括号内的都可以,只能取一个
s = 'abc bbc abcbc bc'
print(re.findall('[abc]bc', s))  #['abc', 'bbc', 'abc']

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

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

# {num}:前面的一个字符num个
s = 'abccabc abccc'
print(re.findall('abc{3}', s))  #['abccc']

# {1,2}:前面的一个字符少于等于2个
s = 'abccabc abccc'
print(re.findall('abc{1,2}', s))  #['abcc', 'abcc']
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!