Python 将字符串变量转换为 UUID 类型

Python convert str variable to UUID type

我正在尝试将字符串变量转换为 UUID 类型。在线教程指向以下代码

import uuid
delete_uuid = "5d27bf88-f3dd-4e95-89c1-f200c8484b42"
your_uuid_string = uuid.UUID(delete_uuid).hex

print(type(your_uuid_string))

但输出仍然是str类型。 请指导

如果您需要获取 UUID 对象作为输出,您需要删除 .hex,因为 UUID.hex returns a str:

The UUID as a 32-character hexadecimal string.

所以,您可以使用

>>> import uuid
>>> delete_uuid = "5d27bf88-f3dd-4e95-89c1-f200c8484b42"
>>> your_uuid_string = uuid.UUID(delete_uuid)
>>> type(your_uuid_string)
<class 'uuid.UUID'>