随便学学Python-day15-时间和hash和json

day15-时间和hash和json

时间模块

1.时间模块

python和时间相关的模块有两个:time、datetime

2.时间戳

时间戳指的是当前时间到1970年1月1日0时0分0秒(指的是格林威治时间)的时间差(单位是秒)

1)使用时间戳保存时间比使用字符串保存时间所占用的内容要少很多;

2)通过时间戳对时间进行加密更简单

# 1.time()  - 获取当前时间,返回的是时间戳
t1 = time.time()
print(t1, type(t1))    # 1608945703.5747042 <class 'float'>

# 2.
# localtime()  - 获取当前本地时间,返回的是结构体时间
t2 = time.localtime()
print(t2, type(t2))
print(t2.tm_year)
print(t2.tm_mon)
print(t2.tm_mday)
print(f'{t2.tm_year}-{t2.tm_mon}-{t2.tm_mday} {t2.tm_hour}:{t2.tm_min}:{t2.tm_sec}')

# localtime(时间戳)  -  将时间戳转换成本地的结构体时间
t3 = time.localtime(0)
print(t3)

t4 = time.localtime(1608945703.5747042)
print(t4)
# 练习1:自己封装一个函数,将结构体时间转换成字符串时间,字符串时间的格式是: 'xxxx年xx月xx日 xx:xx:xx'
def str_time(t: time.struct_time):
    return f'{t.tm_year}年{t.tm_mon}月{t.tm_mday}日 {t.tm_hour}:{t.tm_min}:{t.tm_sec}'


# 练习2:自己封装一个函数,将时间戳转换成字符串时间,字符串时间的格式是: 'xxxx年xx月xx日 xx:xx:xx'
def str_time2(t: float):
    t = time.localtime(t)
    return f'{t.tm_year}年{t.tm_mon}月{t.tm_mday}日 {t.tm_hour}:{t.tm_min}:{t.tm_sec}'

3.将结构体时间转换成字符串时间

strftime(时间格式字符串,结构体时间)

"""
时间格式字符串  - 包含时间占位符的字符串
%Y  -   年
%m  -  月
%d  -  日
%H  -  时(24小时制)
%I  -  时(12小时制)
%M  -  分
%S  -  秒
%a  -  星期(英文单词简写)
%A  -  星期(英文单词全拼)
%w  -  星期(数字值)
%b  -  月份(英文简写)
%B  -  月份(英文全拼)
%p  -  上午或者下午(AM/PM)
"""
t5 = time.localtime()
# xxxx年xx月xx日
s_t5 = time.strftime('%Y年%m月%d日', t5)
print(s_t5)    # 2020年12月26日

# xxxx-xx-xx
s_t5 = time.strftime('%Y-%m-%d', t5)
print(s_t5)     # 2020-12-26

# xxxx/xx/xx
s_t5 = time.strftime('%Y/%m/%d', t5)
print(s_t5)     # 2020/12/26

# xx点xx分
s_t5 = time.strftime('%H点%M分', t5)
print(s_t5)

s_t5 = time.strftime('%c', t5)
print(s_t5)      # Sat Dec 26 10:38:22 2020

# 星期几 上午时:分
s_t5 = time.strftime('%a %p%H:%M', t5)
print(s_t5)

4.sleep(时间) - 程序暂停指定时间(单位秒)

time.sleep(2)
print('===========')

datetime

time - 时分秒

date - 年月日

date - 年月日时分秒

from datetime import time, date, datetime, timedelta

t1 = date.today()
print(t1, t1.year, t1.month, t1.day)

t2 = datetime.now()
print(t2)


t3 = datetime(2020, 12, 31, 23, 58, 40)
print(t3)   # 2020-12-31 23:58:40

# 让时间t3加20秒
print(t3 + timedelta(seconds=20))  # 2020-12-31 23:59:00

# 让时间t3加2分钟
print(t3 + timedelta(minutes=2))    # 2021-01-01 00:00:40

# 让时间t3加2天
print(t3 + timedelta(days=2))    # 2021-01-02 23:58:40

# 让时间t3减2天
print(t3 - timedelta(days=2))     # 2020-12-29 23:58:40

hash摘要

hashlib是python自带的一个专门提供hash加密的模块

1.hash加密的特点

1)同一个数据通过同一个加密算法得到的结果是一样的(加密后的结果叫密文或者摘要)

2)加密后的结果不可逆

3)不同大小的数据通过相同的算法生成的摘要的长度是一样的

2.应用场景

1)创建数据不可逆的密文(加密)

2)验证数据的完整性和是否被修改

3.怎么生成摘要

1)根据加密算法创建hash对象

2)添加加密对象

hash对象.update(数据)

数据 - 必须是bytes对象

3)生成摘要(生成密文)

hash对象.hexdigest()

# 生成图片摘要
hash = hashlib.md5()
with open('宝儿姐.jpg', 'rb') as f:
    hash.update(f.read())
    print(hash.hexdigest())


# 生成文本文件的摘要
hash = hashlib.md5()
with open('01-回顾.py', 'rb') as f:
    b4 = f.read()
    hash.update(b4)
    print(hash.hexdigest())


hash = hashlib.md5()
with open('/Users/yuting/Desktop/test.docx', 'rb') as f:
    b3 = f.read()
    hash.update(b3)
    print(hash.hexdigest())

4.补充:字符串和二进制之间的相互转换

1)字符串转二进制

str1 = 'hello world!'
b1 = bytes(str1, encoding='utf-8')
print(type(b1))

# b.字符串.encode()
b2 = str1.encode()
print(type(b2))

2)二进制转字符串

# a.str(二进制, encoding='utf-8)
# b.二进制.decode()
s1 = str(b1, encoding='utf-8')
print(s1, type(s1))

s2 = str(b4, encoding='utf-8')
print(s2)

s3 = b1.decode()
print(s3)

其他模块

cmath是专门为复数提供数学功能的模块

import math, cmath

result = math.fabs(-89)
print(result)

# floor(浮点数)  - 向小取整
result = math.floor(-2.65)
print(result)

# ceil(浮点数)  -  向大取整
result = math.ceil(4.166)
print(result)

# round(浮点数)  - 四舍五入
result = round(5.17)
print(result)

json数据

1.什么是json

json是一种数据格式:

1)一个json有且只有一个数据

2)这个唯一的数据必须是json支持的数据类型的数据

2.json支持的数据类型

1)数字类型:包括所有的数字,整数、浮点数、正数、负数…,表示的时候直接写,支持科学技术法

2)字符串:只能使用双引号,支持转义字符。“abc”, “abc\n123”, “\u4e00abc”

3)布尔:只有true和false两个值。表示的时候直接写

4)数组:相当于python的列表,[元素1, 元素2, 元素3,…]

5)字典:相当于python的字典,{键1:值1, 键2:值2,…}, 注意:键只能是字符串

6)空值:null

3.json数据转python

数字 - 整型、浮点型

字符串 - 字符串

布尔 - 布尔;true -> True, false -> False

数组 - 列表

字典 - 字典

null - None

# json.loads(数据)  - 字符串;数据指的是json格式的字符串(json格式的字符串:字符串去掉引号之后本身就是一个合法是json数据)
# 'abc' - 不是json格式字符串    '123'  -   是json格式字符串   '[10, 20, 30]'  -   是json格式字符串
# '"abc"'  - 是json格式字符串   'True'  -   不是json格式字符串   '{"10": 20, "name":100}'  -   是json格式字符串
result = json.loads('"abc"')
print(result, type(result))   # 'abc' <class 'str'>

result = json.loads('123')
print(type(result))      # <class 'int'>

result = json.loads('true')
print(result, type(result))    # True <class 'bool'>

result = json.loads('["hello", 123, false, null]')
print(result)    # ['hello', 123, False, None]

result = json.loads('{"name": "张三", "age": 18}')
print(result)    # {'name': '张三', 'age': 18}
print(result['name'])

4.python数据转json

int、float --> 数字

布尔 --> 布尔;True -> true,False -> false

字符串 --> 字符串,引号变成双引号

列表、元组 --> 数组

字典 --> 字典

None --> null

# json.dumps(python数据)    -  将python数据转换成json格式的字符串
# 120  ->  '120'
# 'abc'  -> '"abc"'
# ['abc', 120, True]   ->  '["abc", 120, true]'
# {10, 20, 30}  -> 报错!
result = json.dumps(120)
print(result, type(result))      # '120' <class 'str'>

result = json.dumps('abc')
print(result, type(result))      # '"abc"' <class 'str'>

result = json.dumps([120, 'hello', False, None])
print(result, type(result))      # '[120, "hello", false, null]'  <class 'str'>

result = json.dumps((120, 'hello', False, None))
print(result)      # '[120, "hello", false, null]'

result = json.dumps({'name': '张三', 10: 100})
print(result)    # '{"name": "\u5f20\u4e09", "10": 100}'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值