python str字符串转uuid实例

作者:Hayley-L 时间:2021-12-31 20:15:54 

uuid str int 之间的转换


import uudi

#str 转 uuid
uuid.UUID('12345678123456781234567812345678')
uuid.UUID(hex='12345678123456781234567812345678')
uuid.UUID('{12345678-1234-5678-1234-567812345678}')
uuid.UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
#Out:UUID('12345678-1234-5678-1234-567812345678')

uuid.UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
#Out:UUID('12345678-1234-5678-1234-567812345678')

#int 转 uuid
uuid.UUID(int=0x12345678123456781234567812345678)
#Out:UUID('12345678-1234-5678-1234-567812345678')

#uuid 转 str
str(uuid.uuid4())
#Out:'a0565f88-b20a-4cc1-a6de-11f046bb7100'
type(str(uuid.uuid4()))
#Out:str
python的uuid模块提供UUID类和函数uuid1(), uuid3(), uuid4(), uuid5() 来生成1, 3, 4, 5各个版本的UUID

uuid.uuid1([node[, clock_seq]]) : 主机ID, 序列号, 和时间戳来生成UUID, 可保证全球范围的唯一性
uuid.uuid3(namespace, name) : 基于命名空间和名字的MD5散列值
uuid.uuid4() : 基于随机数
uuid.uuid5(namespace, name) : 基于命名空间和名字的SHA-1散列值,同uuid3

补充拓展:python字符串和time互转与时间的加减另加uuid

咱们看代码吧!


# -*-coding:utf-8 -*-
__author__ = "ZJL"

import uuid,time,datetime

#uuid4产生32位随机字母加数字
print(str(uuid.uuid4()).replace("-",""))
#uuid3产生基于名字的MD5散列值
print(str(uuid.uuid3(uuid.NAMESPACE_DNS,"username")).replace("-",""))

#time转字符串
time_num = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print(time_num)

#字符串转time
t = time.strptime(time_num, '%Y-%m-%d %H:%M:%S')
y,m,d,H,M,S = t[:6]
print(t)
print(datetime.datetime(y,m,d,H,M,S))

#时间的加减
now_time = datetime.datetime.now()
#当前时间加半小时
yes_time = now_time + datetime.timedelta(hours=+0.5)
#比较时间大小
if now_time>yes_time:
 print("ok")
else:
 print("no")
#当前时间减一天
# yes_time = now_time + datetime.timedelta(days=-1)
yes_time_nyr = yes_time.strftime('%Y-%m-%d %H:%M:%S')
print(yes_time_nyr)

结果:

python str字符串转uuid实例


import time, datetime
#一个月前
today1 = datetime.datetime.today()
astmonth = datetime.datetime(today1.year, (today1.month - 1), today1.day, today1.hour, today1.minute,today1.second)

来源:https://blog.csdn.net/weixin_33127753/article/details/90766445

标签:python,str字符串,uuid
0
投稿

猜你喜欢

  • python通过加号运算符操作列表的方法

    2023-11-12 13:44:04
  • 详解python内置常用高阶函数(列出了5个常用的)

    2023-01-11 18:52:19
  • Web2.0电子商务网站的交互设计

    2009-05-15 12:08:00
  • Microsoft SQL Server数据库SA权限总结

    2009-01-06 11:32:00
  • python使用wxpy实现微信消息防撤回脚本

    2023-08-22 21:21:58
  • CSS 针对 Safari(WebKit)的 CSS 注意事项 3

    2008-09-15 18:36:00
  • 在ASP.NET 2.0中操作数据之三十九:在DataList的编辑界面里添加验证控件

    2023-07-06 02:02:48
  • 利用Python实现简单的相似图片搜索的教程

    2023-10-24 18:45:36
  • python turtle绘图命令及案例

    2022-04-29 10:26:58
  • 用ASP创建多栏选项列表SELECT

    2007-10-08 13:18:00
  • win10环境下配置vscode python开发环境的教程详解

    2022-09-06 21:19:49
  • FCKeditor 编辑器实战技巧 Ⅰ

    2008-10-08 10:22:00
  • Pytest+request+Allure实现接口自动化框架

    2023-08-12 17:29:33
  • asp中文件与文件夹常用处理函数(文件后缀、创建文件等)

    2011-02-20 11:00:00
  • 把JS与CSS写在同一个文件里

    2007-12-16 15:29:00
  • vertical-align表单元素垂直对齐

    2009-07-27 13:02:00
  • python实现监控指定进程的cpu和内存使用率

    2023-08-23 02:21:17
  • 利用python爬取斗鱼app中照片方法实例

    2022-01-03 17:40:39
  • oracle sqlplus 常用命令大全

    2009-05-24 19:47:00
  • 使用MySql和php出现中文乱码的解决方法

    2009-01-14 12:02:00
  • asp之家 网络编程 m.aspxhome.com