day15-时间和hash和json

时间模块

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

  2. 时间戳

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

    优点:

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

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

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

    2. localtime() - 获取当前本地时间,返回的是结构体时间

      localtime(时间戳) - 将时间转换成本地的结构体时间

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

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

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

datetime

from datetime import time, date, datetime, timedelta
# time - 时分秒
# date - 年月日
# datetime - 年月日时分秒
t1 = date.today()
print(t1, t1.year, t1.month, t1.day)	# 2020-12-26 2020 12 26

t2 = datetime.now()
print(t2)	# 2020-12-26 17:50:13.300294
	
t3 = datetime(2020, 12, 31, 23, 59, 10)
print(t3)   # 2020-12-31 23:59:10

print(t3 + timedelta(seconds=40))   # 2020-12-31 23:59:50
print(t3 + timedelta(days=1))   # 2021-01-01 23:59:10

hash摘要

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

  1. hash加密的特点:

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

    1. 创建数据不可逆的密位(加密)
    2. 验证数据的完整性和是否被修改
  3. 怎么生成摘要

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

      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
      
    4. 生成图片摘要

      hash = hashlib.md5()
      with open('图片路径', 'rb') as f:
          hash.update(f.read())
          print(hash.hexdigest())
      
    5. 生成文本文件摘要

      hash = hashlib.md5()
      with open('01-回顾.py', 'rb') as f:
          hash.update(f.read())
          print(hash.hexdigest()) # 584e96244fb3de4e51233ee5306f37d0
      
  4. (补充)字符串和二进制之间的相互转换

    1. 字符串转二进制 - 编码

      a.bytes(字符串, encoding=‘utf-8’)

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

      b.字符串.encode()

      b2 = str1.encode()
      print(type(b2))     # <class 'bytes'>
      
    2. 二进制转字符串 - 解码

      a.str(二进制, encoding=‘utf-8’)

      b.二进制.decode()

      s1 = str(b1, encoding='utf-8')
      print(s1, type(s1))     # hello world! <class 'str'>
      
      s2 = b1.decode()
      print(s2, type(s2))     # hello world! <class 'str'>
      

其他模块

cmath是专门为复数提供数学功能的模块
import math, cmath, os

print(math.ceil(1.2))   # 2 - 向上取整
print(math.floor(1.2))  # 1 - 向下取整

json数据

  1. 什么是json

    json是一种数据格式: 一个json有且只有一个数据;这个唯一的数据必须是json支持的数据类型的数据

  2. json支持的数据类型

    1. 数字类型:包括所有的数字,包含整数、浮点数、正数、负数…表示的时候直接写,支持科学计数法
    2. 字符串:用双引号引起来的文本数据(只能使用双引号),支持转义字符。 - “abc”, ‘abc\n123’, “\u4e00abc”
    3. 布尔:只有ture和false两个值,表示的时候直接写
    4. 数组:相当于列表,[元素1, 元素2, 元素3, …]
    5. 字典:相当于python的字典,{键1:值1, 键2:值2, 键3:值3, …}, 注意:键只能是字符串
    6. 空值:null
  3. json数据转python

    数字 – 整型、浮点型

    字符串 – 字符串

    布尔 – 布尔:true -> True; false -> False

    数组 – 列表

    字典 – 字典

    null – None

json.loads(json数据) - json数据指的是jison格式的字符串(字符串去掉引号之后,本身就是一个合法的json数据)

‘abc’ - 不是json格式字符串

’ “abc” ’ - 是json格式字符串

‘123’ – 是json格式字符串

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

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

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

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

result = json.loads('{"name":"小明", "age":18}')
print(result, type(result))  # {'name': '小明', 'age': 18} <class 'dict'>
  1. 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]’

    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, False, None, 'hello'])
    print(result, type(result))  # '[120, false, null, "hello"]' <class 'str'>
    
    result = json.dumps({'name':'张三', 10:100})
    print(result)  # '{"name": "\u5f20\u4e09", "10": 100}'
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值