gpt4 book ai didi

python - 如何在 Python 中创建 GUID/UUID

转载 作者:IT老高 更新时间:2023-10-28 12:01:00 29 4
gpt4 key购买 nike

如何在 Python 中创建独立于平台的 GUID/UUID?我听说有一种在 Windows 上使用 ActivePython 的方法,但它只是 Windows,因为它使用 COM。有没有使用纯 Python 的方法?

最佳答案

The uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122.

If all you want is a unique ID, you should probably call uuid1() or uuid4(). Note that uuid1() may compromise privacy since it creates a UUID containing the computer’s network address. uuid4() creates a random UUID.

UUID 版本 6 和 7 - 用于现代应用程序和数据库的新通用唯一标识符 (UUID) 格式 (draft) rfc - 可从 https://pypi.org/project/uuid6/ 获得

文档:

示例(适用于 Python 2 和 3):

>>> import uuid

>>> # make a random UUID
>>> uuid.uuid4()
UUID('bd65600d-8669-4903-8a14-af88203add38')

>>> # Convert a UUID to a string of hex digits in standard form
>>> str(uuid.uuid4())
'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'

>>> # Convert a UUID to a 32-character hexadecimal string
>>> uuid.uuid4().hex
'9fe2c4e93f654fdbb24c02b15259716c'

关于python - 如何在 Python 中创建 GUID/UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/534839/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com