打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python笔记第五次

时间戳、时间元组、时间字符串之间的转换

 import random
print(random.randint(1,43))
 import time 

1.将时间戳-> 时间元组

gmtime -> utc 国际标准时间
localtime -> 本地时间
72047023

 print(time.gmtime(72047023)) print(time.localtime(72047023))

2.将时间元组转为时间字符串

print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(0))) print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(0)))

3.将时间字符串转为时间元组

 print(time.strptime("2200-11-11 12:30:29","%Y-%m-%d %H:%M:%S"))

4.将时间元组转为时间戳

print(time.mktime(time.strptime("2200-11-11 12:30:29","%Y-%m-%d %H:%M:%S")))

时间戳 <-> 时间元组 <-> 时间字符串

import datetime 

datetime是time模块的一个封装

print(datetime.datetime.now())

哈希加密

 import hashlib 

md5 可以被暴力破解
code = hashlib.sha512(“123456”.encode(“utf-8”))
code.update("(#‵′)靠つ".encode(“utf-8”)) # "加盐"操作防止暴力破解
print(code.hexdigest())

json 模块

json 原本是JavaScript中的一种数据格式,类似于Python中的字典
Python中的对象 -> JSON格式的字符串 (序列化)
JSON格式的字符串 -> Python中的数据对象 (反序列化操作)

dump 导出操作 python对象-> json字符串
load 导入操作 json字符串-> python对象

import json  dic = { 'name':'zj','age':18,'tup' : ('a',1,2,3,4,True,False),}

json.dumps(obj) 字符串导出
json.dump(obj,f) 文件写入 with
open(“data.json”,“w”,encoding=“utf-8”) as f: json.dump(dic,f)

data = json.loads(’{“pi”:3.141592653}’)

print(data[“pi”]*3)

json.loads(字符串) -> python对象 json.load(文件对象) -> python对象 with
open(“data.json”,“r”,encoding=“utf-8”) as f: data = json.load(f)

print(data[“name”])

来源:https://www.icode9.com/content-1-768301.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python3 日期和时间 - 心得与示例代码(40)
C站最全Python库总结丨标准库+高级库
python常用模块详解(一)
Python常用模块大全(总结)
当我学完Python我学了些什么
python 中的小问题(持续更新中)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服