How to create a GUID/UUID in Python

前端 未结 8 1089
情话喂你
情话喂你 2020-11-22 16:47

How do I create a GUID in Python that is platform independent? I hear there is a method using ActivePython on Windows but it\'s Windows only because it uses COM. Is there a

8条回答
  •  没有蜡笔的小新
    2020-11-22 17:35

    The uuid module, in Python 2.5 and up, provides RFC compliant UUID generation. See the module docs and the RFC for details. [source]

    Docs:

    • Python 2: http://docs.python.org/2/library/uuid.html
    • Python 3: https://docs.python.org/3/library/uuid.html

    Example (working on 2 and 3):

    >>> import uuid
    >>> uuid.uuid4()
    UUID('bd65600d-8669-4903-8a14-af88203add38')
    >>> str(uuid.uuid4())
    'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'
    >>> uuid.uuid4().hex
    '9fe2c4e93f654fdbb24c02b15259716c'
    

提交回复
热议问题