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

gan*_*ukh 0 python uuid

我在 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'
>>> 
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得uuid?由连字符分隔并将字符串分为四个部分: daf59b684-d6a11x-z9a9c3402-8611c679

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

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

UUID-Docs链接

dec*_*eze 5

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

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

请注意,这是明确记录的

str(uuid)12345678-1234-5678-1234-567812345678以 32 个十六进制数字表示 UUID的形式返回一个字符串。