如何在 Python 中创建独立于平台的 GUID/UUID?我听说有一种在 Windows 上使用 ActivePython 的方法,但它只是 Windows,因为它使用 COM。有没有使用普通Python的方法?
The uuid模块 https://docs.python.org/3/library/uuid.html提供不可变的 UUID 对象(UUID 类)和函数uuid1() https://docs.python.org/3/library/uuid.html#uuid.uuid1, uuid3() https://docs.python.org/3/library/uuid.html#uuid.uuid3, uuid4() https://docs.python.org/3/library/uuid.html#uuid.uuid4, uuid5() https://docs.python.org/3/library/uuid.html#uuid.uuid5用于生成版本 1、3、4 和 5 UUID,如RFC 4122 https://www.rfc-editor.org/rfc/rfc4122.
如果您想要的只是一个唯一的 ID,您可能应该致电uuid1()
or uuid4()
.
注意uuid1()
可能会损害隐私,因为它会创建包含计算机网络地址的 UUID。
uuid4()
创建一个随机 UUID。
UUID 版本 6 和 7 -用于现代应用程序和数据库的新通用唯一标识符 (UUID) 格式 (draft) rfc https://uuid6.github.io/uuid6-ietf-draft/- 可从https://pypi.org/project/uuid6/ https://pypi.org/project/uuid6/
Docs:
- Python 2 https://docs.python.org/2/library/uuid.html
- Python 3 https://docs.python.org/3/library/uuid.html
示例(适用于 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'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)