如何在Python中创建8位数字的唯一ID? [英] How to create a 8 digit Unique ID in Python?

查看:902
本文介绍了如何在Python中创建8位数字的唯一ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个用于练习的Web应用程序,它是一个工作门户。我想为作业创建8位数的唯一ID,这对于最终用户将是可见的。 ID可以包含数字和字母。模式必须是8位数字,且没有连字符或连字符,例如 XXXXXXXX

I am making a web app for practice, it is a job portal. I want to create 8 digit unique IDs for job which will be visible to the end user. Ids can have numbers and alphabets. Pattern needs to be 8 digits without hyphens or dashes like XXXXXXXX.

我知道python中存在UUID字样。但是他们似乎会以其特定格式生成ID。有没有一种方法可以获取我所需格式的ID?如果不能,请有人指导我怎么做?

I know there is UUID thing in python. But they seem to generate the id in their specific format. Is there a way to get the id in my required format? If not can someone please guide me on how to go about it?

我知道有一个类似的线程如何在Python中生成唯一ID?;但这并不能具体回答我的问题。

I know there is a similar thread How can I generate a unique ID in Python?; but it does not answer my question specifically. It did not answer what I was trying to achieve.

预先感谢。

推荐答案

好吧,您可以使用 uuid.hex

import uuid
uuid.uuid4().hex[:8]  # Might reduce uniqueness because of slicing

或者Django也具有帮助功能 get_random_string 接受两个参数 length ( default = 12) allowed_chars

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)

这篇关于如何在Python中创建8位数字的唯一ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆