如何在Python中创建独立于平台的GUID?我听说有一种在Windows上使用ActivePython的方法,但它只是Windows,因为它使用COM.有没有使用普通Python的方法?
如何在Python中创建独立于平台的GUID?我听说有一种在Windows上使用ActivePython的方法,但它只是Windows,因为它使用COM.有没有使用普通Python的方法?
uuid module提供了不可变的UUID对象(UUID类)和函数
uuid1()
、uuid3()
、uuid4()
、uuid5()
,用于生成RFC 4122中指定的版本1、3、4和5 UUID.
如果您想要的只是一个唯一的ID,您可能应该拨打
uuid1()
或uuid4()
.Note that 100 may compromise privacy since it creates a UUID containing the computer’s network address.uuid4()
创建随机UUID.
文件:
示例(适用于Python 2和Python 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'