python uuid生成唯一id或str的最简单案例
作者:lyl-hello 时间:2021-06-07 00:40:45
介绍:
UUID是128位的全局唯一标识符,通常由32字节的字符串表示。
使用:
import uuid
print uuid.uuid1()
14bfe806-f1c7-11e6-83b5-0680f3c45093
uuid1()
——基于MAC地址、当前时间戳、随机数生成。
import uuid
print uuid.uuid3(uuid.NAMESPACE_DNS, 'test')
45a113ac-c7f2-30b0-90a5-a399ab912716
uuid3()
——基于名字的MD5散列值。
import uuid
print uuid.uuid4()
8a602450-b27b-4d47-851d-3158da061153
uuid4()
——基于随机数,有一定的重复概率。
import uuid
print uuid.uuid5(uuid.NAMESPACE_DNS, 'test')
4be0643f-1d98-573b-97cd-ca98a65347dd
uuid5()
——基于名字的SHA-1散列值。与uuid3用法相同
补充:
没有uuid2函数
扩展:Python使用haslib.md5生成唯一ID
在hashlib库的hash算法中,提供了很多加密算法,有 sha1()、sha224()、sha256()、sha384()、sha512()、blake2b()和 blake2s()、md5(),这些方法都通过统一接口返回一个对象,例如,使用sha256()可以创建一个SHA-256的哈希对象。
import hashlib
md = haslib.md5(salt) # bytes
md.update('') # encode
re = md.hexdigest()
print(re)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_37023538/article/details/55049952
标签:python,uuid,id,str
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3安装pip工具的详细步骤
2021-09-27 15:38:09
![](https://img.aspxhome.com/file/2023/3/72923_0s.png)
python迭代dict的key和value的方法
2021-11-03 16:04:17
Pycharm学习教程(3) 代码运行调试
2023-04-11 00:01:18
![](https://img.aspxhome.com/file/2023/3/109453_0s.png)
python GUI计算器的实现
2021-11-30 01:00:53
![](https://img.aspxhome.com/file/2023/8/87948_0s.png)
Asp中Scripting.TextStream 对象介绍
2007-11-02 12:19:00
解决DataFrame排序sort的问题
2023-07-04 15:37:35
利用Python程序读取Excel创建折线图
2021-05-29 00:36:33
![](https://img.aspxhome.com/file/2023/9/85219_0s.png)
python 中的int()函数怎么用
2021-05-21 17:52:41
网页版权的正确写法
2007-09-22 09:13:00
python numpy库之如何使用matpotlib库绘图
2023-02-07 22:22:24
![](https://img.aspxhome.com/file/2023/2/104392_0s.png)
Python中的函数参数传递问题
2023-09-28 05:38:12
![](https://img.aspxhome.com/file/2023/0/62290_0s.png)
python中random模块详解
2023-07-30 02:29:23
![](https://img.aspxhome.com/file/2023/1/61211_0s.png)
如何判断电子邮件的地址格式是否正确?
2010-01-12 20:12:00
python 内置函数filter
2023-01-07 21:53:37
matplotlib绘制两点间连线的几种方法实现
2021-07-27 09:02:24
![](https://img.aspxhome.com/file/2023/0/107240_0s.jpg)
Opencv中的cv2.calcHist()函数的作用及返回值说明
2021-03-26 10:27:56
![](https://img.aspxhome.com/file/2023/7/95177_0s.png)
Python处理JSON时的值报错及编码报错的两则解决实录
2023-11-10 07:12:07
ASP基础教程:常用的 ASP ActiveX 组件
2008-10-14 15:15:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081213132956582.gif)
Mini,又个 Javascript 选择器
2009-10-06 14:48:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/medium-36s.jpg)
asp如何在数据库中用好Transaction?
2010-06-22 21:07:00