网络编程
位置:首页>> 网络编程>> Python编程>> python 根据时间来生成唯一的字符串方法

python 根据时间来生成唯一的字符串方法

作者:云中不知人  发布时间:2022-12-25 14:49:48 

标签:python,唯一,字符串

我们很多时候,特别是在生成任务的时候,都需要一个唯一标识字符串来标识这个任务,比较常用的有生成uuid或者通过时间来生成。uuid的话可以直接通过uuid模块来生成。如果是时间的话,可以这么写:


def tid_maker():
return '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())

这里的时间精确到了微妙,一般来说不会重复,如果想更安全点,可以在后面多加几个随机字符,例如:


def tid_maker():
return '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())+''.join([str(random.randint(1,10)) for i in range(5)])

来源:https://blog.csdn.net/u011085172/article/details/80676066

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com