如何在python中将str字符串转换成uuid

发布时间:2021-02-26 16:24:03 作者:戴恩恩
来源:亿速云 阅读:648

这篇文章主要为大家详细介绍了如何在python中将str字符串转换成uuid,文中示例代码介绍的非常详细,具有一定的参考价值,发现的小伙伴们可以参考一下:

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

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)

以上就是亿速云小编为大家收集整理的如何在python中将str字符串转换成uuid,如何觉得亿速云网站的内容还不错,欢迎将亿速云网站推荐给身边好友。

推荐阅读:
  1. 如何在python中将datetime转换成pd.to_datetime
  2. 如何在python中将字符串转换成json

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python str uuid

上一篇:如何在python中使用html2text库将HTML转换成markdown

下一篇:如何在python中把字符串转化成numpy浮点数组

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》