作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用uuid
模块,我可以使用以下方法生成一个唯一的字符串(例如c389fa3c-3a5c-4d8d-ac92-9b70f2bbe0b5
):
import uuid
result = uuid.uuid4()
print result
其结果是:
<class 'uuid.UUID'>
现在,为了获取生成的字符串,我需要调用 str()
函数:
uuid_string = str(result)
print uuid_string
打印:
c389fa3c-3a5c-4d8d-ac92-9b70f2bbe0b5
我想知道是否有更短的方法将 uuid 文本字符串生成为字符串(无需使用 str()
函数)。
最佳答案
您可以使用.hex
属性来获取字符串值,无需-
In [1]: import uuid
In [2]: result = uuid.uuid4()
In [3]: result.hex
Out[3]: '536bc225eb6d47589b1858f265b809b1'
In [4]: print(result.hex)
536bc225eb6d47589b1858f265b809b1
相关文档如下:
UUIDs have these read-only attributes:
bytes the UUID as a 16-byte string (containing the six
integer fields in big-endian byte order)
bytes_le the UUID as a 16-byte string (with time_low, time_mid,
and time_hi_version in little-endian byte order)
fields a tuple of the six integer fields of the UUID,
which are also available as six individual attributes
and two derived attributes:
time_low the first 32 bits of the UUID
time_mid the next 16 bits of the UUID
time_hi_version the next 16 bits of the UUID
clock_seq_hi_variant the next 8 bits of the UUID
clock_seq_low the next 8 bits of the UUID
node the last 48 bits of the UUID
time the 60-bit timestamp
clock_seq the 14-bit sequence number
hex the UUID as a 32-character hexadecimal string
int the UUID as a 128-bit integer
urn the UUID as a URN as specified in RFC 4122
variant the UUID variant (one of the constants RESERVED_NCS,
RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
version the UUID version number (1 through 5, meaningful only
when the variant is RFC_4122)
关于python - 如何生成uuid字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55150843/
我是一名优秀的程序员,十分优秀!