15时间和hash,json

时间和hash,json

  • 时间模块

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

时间戳

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

1)使用时间戳保存时间比使用字符串保存时间所占用的内容要少很多
1608946200.0289612 (保存时大概需要4个字节) -> 包含了年,月,日,时,分,秒,毫秒
‘2020-12-26 09:32:44’(保存的时候至少需要18个字节)

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

1.time() - 获取当前时间,返回的是时间戳

import time

t1 = time.time()
print(t1,type(t1))

2.localtime() - 获取当前本地时间

t2 = time.localtime()
print(t2, type(t2))
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(1608946200.0289612)
print(t4)

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

# 练习:将时间戳转换成字符串时间
def func2(timestamp):
    t2 = time.localtime(timestamp)
    print(f'{t2.tm_year}年{t2.tm_mon}月{t2.tm_mday}日 {t2.tm_hour}:{t2.tm_min}:{t2.tm_sec}')

func1(time.localtime())
func2(time.time())

3.将结构体时间转换成字符串时间
strftime(时间格式字符串,结构体时间)

%Y - 年
%m - 月
%d - 日
%H - 时(24)
%I - 时(12)
%M - 分
%S - 秒
%a - 星期(英文单词简写)
%A - 星期(英文单词全拼)
%w - 星期(数字)
%b - 月份(简写)
%B - 月份(全拼)
%p - 上午或者下午(AM/PM)

t5 = time.localtime(0)
s_t5 = time.strftime('%Y{y}%m{m}%d{d} %H{h}%M{f}%S{s}', t5).format(y='年',m='月',d='日',h='时',f='分',s='秒')
print(s_t5)

# s_t5 = time.strftime('%Y-%m-%d')
# print(s_t5)

s_t5 = time.strftime('%a %p%H:%M',t5)
print(s_t5)

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

time.sleep(2)
print('123')
  • datetime
from datetime import time,date,datetime,timedelta
# time 时分秒
# date 年月日
# datetime 年月日时分秒
t1 = date.today()
print(t1,t1.year,t1.month,t1.day)

t2 = datetime.now()
print(t2)

# 让时间t3加20秒
t3 = datetime(2020,12,31,23,58,40)
print(t3 + timedelta(seconds=20))
  • hash摘要

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

  1. 同一个数据通过同一个加密算法得到的结果是一样的(加密后的结果叫密文或者摘要)
  2. 加密后的结果不可逆
  3. 不同大小的数据通过相同的算法生成摘要的长度是一样的

2.应用场景

  1. 创建数据不可逆的密文(加密)
  2. 验证数据的完整性和是否被修改

3.怎么生成摘要

# 1) 根据加密算法创建hash对象
import hashlib
hash = hashlib.md5() # 常见hash算法:md5,sha相关
# 2) 确定对象
# hash对象.update(数据)
# 数据 - 必须是bytes对象
hash.update(bytes('123456', encoding='utf-8'))

# 3) 生成摘要(生成密文)
# hash对象.hexdigest()
result = hash.hexdigest()
print(result) # e10adc3949ba59abbe56e057f20f883e

# 生成文本文件的摘要
with open('01-时间模块.py','rb') as f:
    hash.update(f.read())
    print(hash.hexdigest())

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

# 1)字符串转二进制
# a.bytes(字符串,encoding='utf-8')
str1 = 'hello'
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 = b1.decode()
print(s2
  • 其他模块
import math,cmath

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

result = math.copysign(-5, 9)
print(result)

result = math.tan(math.pi/4)
print(result)

result = math.degrees(math.pi/2)
print(result)

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\n123” “\u4e00”
3)布尔:只有true和false 两个值。表示的时候直接写
4)数组:相当于python列表,[元素1,元素2…]
5)字典:相当于python字典,{键1:值1,键2:值2…}注意:键只能是字符串
6)空值:null

3.python数据转json
json.loads(json数据) - json数据指的是json格式的字符串()
‘abc’ - 不是json格式字符串
‘“abc”’ - 是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']) # 张三
# 练习:获取所有国家的名字和对应的死亡人数,并且按照死亡人数从大到小排列
# [('中国',2345),('韩国',345)]
with open('test.txt',encoding='utf-8') as f:
    text = f.read()
result = json.loads(text)
newslist = result['newslist']
data = [(x['provinceName'],x['deadCount']) for x in newslist]
data.sort(key=lambda item:item[1],reverse=True)
print(data)

4.json数据转python

python json
int,float -> 数字
布尔 -> 布尔;True - true False - false
字符串 -> 字符串,引号变双引号
列表 元组 -> 数组
字典 -> 字典
None -> null

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值