如何在Python中创建GUID/UUID [英] How to create a GUID/UUID in Python

查看:359
本文介绍了如何在Python中创建GUID/UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中创建与平台无关的GUID?我听说有一种在Windows上使用ActivePython的方法,但这仅是Windows,因为它使用COM.有使用普通Python的方法吗?

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 method using plain Python?

推荐答案

Python 2.5及更高版本中的uuid模块提供符合RFC的UUID 一代.有关详细信息,请参见模块文档和RFC. []

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

文档:

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

示例(适用于2和3):

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'

这篇关于如何在Python中创建GUID/UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆