如何使用 uuid-package for Python 3.6 创建带有破折号而不是没有破折号的 UUID?

How to Create UUID with dashes, instead of without dashes using uuid-package for Python 3.6?

我在 Odoo 框架中使用 python v3.6。

我想生成uuid,所以我使用uuid如下:

:~$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uuid
>>> id =  uuid.uuid1().hex
>>> id
'daf59b684d6a11xz9a9c34028611c679'
>>> 

如何获取uuid?用连字符分隔并将字符串分为四个部分: daf59b684-d6a11x-z9a9c3402-8611c679

我试着在 SO 上找到它,但没有找到,而是得到了一个反向的,与我的要求相反 link

如何在 uuid 字符串之间创建连字符,就像这样??

UUID-文档link.

您只想将 UUID 转换为字符串:

>>> import uuid
>>> print(uuid.uuid1())
74ba3af4-4d6d-11ea-989f-784f435149ee
>>> u = str(uuid.uuid1())
>>> u
'7d7b626c-4d6d-11ea-989f-784f435149ee'

注意这是clearly documented:

str(uuid) returns a string in the form 12345678-1234-5678-1234-567812345678 where the 32 hexadecimal digits represent the UUID.

恐怕您对 UUID 有误解。那些只是数字,128 个二进制数字是它们的固有表示。 hex 属性 只是提供那些格式化为十六进制的字符串。如果你想要破折号在那里,你可以很容易地自己添加它们,因为你可以保证你有 32 个十六进制数字。

但是,请阅读 help(uuid),因为有一种更简单的方法可以获得您想要的内容。只需使用 str(id).

将对象转换为字符串