Python生成短uuid的方法实例详解
时间:2021-06-30 14:12:03
python的uuid都是32位的,比较长,处理起来效率比较低,
本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符,
最后生成的Uuid,只有8位,代码如下:
uuid4,可以换成uuid1
from uuid import uuid4
uuidChars = ("a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z")
def short_uuid():
uuid = str(uuid4()).replace('-', '')
result = ''
for i in range(0,8):
sub = uuid[i * 4: i * 4 + 4]
x = int(sub,16)
result += uuidChars[x % 0x3E]
return result
print(short_uuid())
print(short_uuid())
print(short_uuid())
运行结果如下:
6vT7sxFK
F802Fj8C
s7E3qzmD
总结
以上所述是小编给大家介绍的Python生成短uuid的方法实例详解网站的支持!
来源:https://www.cnblogs.com/xcr1234/archive/2018/05/29/9103461.html
标签:python,uuid
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
2022-10-16 00:45:24
![](https://img.aspxhome.com/file/2023/9/81439_0s.jpg)
Python tkinter事件高级用法实例
2023-12-09 10:28:17
![](https://img.aspxhome.com/file/2023/5/93685_0s.jpg)
asp实现非大小写的替换函数
2010-05-19 21:23:00
Python开发毕设案例之桌面学生信息管理程序
2021-03-02 14:56:08
![](https://img.aspxhome.com/file/2023/2/76752_0s.png)
python 阿里云oss实现直传签名与回调验证的示例方法
2021-12-08 00:30:18
DW表格应用之细线框的制作
2008-02-03 19:00:00
![](https://img.aspxhome.com/file/UploadPic/20082/3/20082319946365s.gif)
德国ebay购头记
2009-04-29 11:10:00
![](https://img.aspxhome.com/file/UploadPic/20094/29/01-96s.jpg)
sklearn线性逻辑回归和非线性逻辑回归的实现
2021-06-16 04:04:31
![](https://img.aspxhome.com/file/2023/3/72213_0s.png)
防注入asp过滤sql特殊字符函数
2007-10-23 17:50:00
Python实现普通图片转ico图标的方法详解
2023-08-13 07:09:36
![](https://img.aspxhome.com/file/2023/0/99600_0s.jpg)
Python实现文件操作帮助类的示例代码
2023-02-14 16:46:57
![](https://img.aspxhome.com/file/2023/2/95672_0s.png)
PHP字符转义相关函数小结(php下的转义字符串)
2023-11-16 14:29:18
跟老齐学Python之网站的结构
2021-08-22 05:55:54
![](https://img.aspxhome.com/file/2023/8/95468_0s.png)
python 字典访问的三种方法小结
2022-03-25 06:07:26
基于python+opencv调用电脑摄像头实现实时人脸眼睛以及微笑识别
2023-07-08 23:31:14
![](https://img.aspxhome.com/file/2023/5/76865_0s.jpg)
Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)
2021-01-23 04:23:13
Python函数中apply、map、applymap的区别
2022-08-11 04:20:36
社区版pycharm创建django项目的方法(pycharm的newproject左侧没有项目选项)
2022-05-26 04:58:36
![](https://img.aspxhome.com/file/2023/2/99682_0s.png)
什么是Python变量作用域
2021-12-25 21:36:24
记录PHP错误日志 display_errors与log_errors的区别
2023-11-14 09:38:29