import string, random
# ''.join() joins the letters from random.choices() into a single Python str of length k.
join=''.join
join(random.choices(string.ascii_letters, k=8)) # Output is like as 'XDCxVAJl'
Resource: Python’s string module contains a number of useful constants: ascii_lowercase, ascii_uppercase, string.punctuation, ascii_whitespace, string.hexdigits and a handful of others.
正确答案
正如here所回答的,这个问题的正确答案是,不可能生成8个字符的uuid,因为uuid定义为16个字节。
uuid的标准在RFC 4221中指定。在format section中,第一个词是:
谢谢你shudipta-sharma注意到我的原始答案不会产生一个通用的唯一id
旧(不正确)答案
您可以只取前八个字符。```Python id1=str(uuid.uuid1())[:8] ```不保证它们都是唯一的。您可以尝试以下方法:
参考:https://realpython.com/python-random/
相关问题 更多 >
编程相关推荐