How to create a 8 digit Unique ID in Python?

旧城冷巷雨未停 提交于 2019-12-05 11:14:37

Well you could use uuid.hex

import uuid
uuid.uuid4().hex[:8]

Or Django also has helper function get_random_string which accepts two parameters length (default=12) and allowed_chars:

from django.utils.crypto import get_random_string
get_random_string(8).lower()

Use os.urandom for the data, and base64 encode it;

In [1]: import os

In [2]: import base64

In [3]: base64.b64encode(os.urandom(6)).decode('ascii')
Out[3]: '6Amtry80'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!