python str字符串轉(zhuǎn)uuid實例
更新時間:2020年03月03日 10:12:35 作者:Hayley-L
這篇文章主要介紹了python str字符串轉(zhuǎn)uuid實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
uuid str int 之間的轉(zhuǎn)換
import uudi
#str 轉(zhuǎn) uuid
uuid.UUID('12345678123456781234567812345678')
uuid.UUID(hex='12345678123456781234567812345678')
uuid.UUID('{12345678-1234-5678-1234-567812345678}')
uuid.UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
#Out:UUID('12345678-1234-5678-1234-567812345678')
uuid.UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
#Out:UUID('12345678-1234-5678-1234-567812345678')
#int 轉(zhuǎn) uuid
uuid.UUID(int=0x12345678123456781234567812345678)
#Out:UUID('12345678-1234-5678-1234-567812345678')
#uuid 轉(zhuǎn) str
str(uuid.uuid4())
#Out:'a0565f88-b20a-4cc1-a6de-11f046bb7100'
type(str(uuid.uuid4()))
#Out:str
python的uuid模塊提供UUID類和函數(shù)uuid1(), uuid3(), uuid4(), uuid5() 來生成1, 3, 4, 5各個版本的UUID
uuid.uuid1([node[, clock_seq]]) : 主機ID, 序列號, 和時間戳來生成UUID, 可保證全球范圍的唯一性
uuid.uuid3(namespace, name) : 基于命名空間和名字的MD5散列值
uuid.uuid4() : 基于隨機數(shù)
uuid.uuid5(namespace, name) : 基于命名空間和名字的SHA-1散列值,同uuid3
補充拓展:python字符串和time互轉(zhuǎn)與時間的加減另加uuid
咱們看代碼吧!
# -*-coding:utf-8 -*-
__author__ = "ZJL"
import uuid,time,datetime
#uuid4產(chǎn)生32位隨機字母加數(shù)字
print(str(uuid.uuid4()).replace("-",""))
#uuid3產(chǎn)生基于名字的MD5散列值
print(str(uuid.uuid3(uuid.NAMESPACE_DNS,"username")).replace("-",""))
#time轉(zhuǎn)字符串
time_num = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print(time_num)
#字符串轉(zhuǎn)time
t = time.strptime(time_num, '%Y-%m-%d %H:%M:%S')
y,m,d,H,M,S = t[:6]
print(t)
print(datetime.datetime(y,m,d,H,M,S))
#時間的加減
now_time = datetime.datetime.now()
#當(dāng)前時間加半小時
yes_time = now_time + datetime.timedelta(hours=+0.5)
#比較時間大小
if now_time>yes_time:
print("ok")
else:
print("no")
#當(dāng)前時間減一天
# yes_time = now_time + datetime.timedelta(days=-1)
yes_time_nyr = yes_time.strftime('%Y-%m-%d %H:%M:%S')
print(yes_time_nyr)
結(jié)果:

import time, datetime #一個月前 today1 = datetime.datetime.today() astmonth = datetime.datetime(today1.year, (today1.month - 1), today1.day, today1.hour, today1.minute,today1.second)
以上這篇python str字符串轉(zhuǎn)uuid實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中關(guān)鍵字nonlocal和global的聲明與解析
這篇文章主要給大家介紹了關(guān)于Python中關(guān)鍵字nonlocal和global的聲明與解析的相關(guān)資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
安裝并免費使用Pycharm專業(yè)版(學(xué)生/教師)
這篇文章主要介紹了安裝并免費使用Pycharm專業(yè)版(學(xué)生/教師),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
基于python圖書館管理系統(tǒng)設(shè)計實例詳解
這篇文章主要介紹了基于python圖書館管理系統(tǒng)設(shè)計實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

