SoFunction
Updated on 2024-11-12

python str string to uuid example

Conversion between uuid str int

import uudi
 
#str to uuid
('12345678123456781234567812345678')
(hex='12345678123456781234567812345678')
('{12345678-1234-5678-1234-567812345678}')
('urn:uuid:12345678-1234-5678-1234-567812345678')
#Out:UUID('12345678-1234-5678-1234-567812345678')
 
(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
#Out:UUID('12345678-1234-5678-1234-567812345678')
 
#int to uuid
(int=0x12345678123456781234567812345678)
#Out:UUID('12345678-1234-5678-1234-567812345678')
 
#uuid to str
str(uuid.uuid4())
#Out:'a0565f88-b20a-4cc1-a6de-11f046bb7100'
type(str(uuid.uuid4()))
#Out:str
python(used form a nominal expression)uuidmodule provisionUUIDClasses and Functionsuuid1(), uuid3(), uuid4(), uuid5() to generate1, 3, 4, 5各个版本(used form a nominal expression)UUID

uuid.uuid1([node[, clock_seq]]) : hostsID, product key (software), 和时间戳to generateUUID, 可保证全球范围(used form a nominal expression)唯一性
uuid.uuid3(namespace, name) : 基于命名空间和名字(used form a nominal expression)MD5hash
uuid.uuid4() : Based on random numbers
uuid.uuid5(namespace, name) : 基于命名空间和名字(used form a nominal expression)SHA-1hash,sameuuid3

Additional extensions: python string and time interconvert with time add and subtract another uuid

Let's look at the code!

# -*-coding:utf-8 -*-
__author__ = "ZJL"
 
import uuid,time,datetime
 
#uuid4 generates 32-bit random letters plus numbers
print(str(uuid.uuid4()).replace("-",""))
#uuid3 generates name-based MD5 hashes
print(str(uuid.uuid3(uuid.NAMESPACE_DNS,"username")).replace("-",""))
 
#time to string
time_num = ('%Y-%m-%d %H:%M:%S',(()))
print(time_num)
 
#String to time
t = (time_num, '%Y-%m-%d %H:%M:%S')
y,m,d,H,M,S = t[:6]
print(t)
print((y,m,d,H,M,S))
 
# Time additions and subtractions
now_time = ()
#♪ The current time plus half an hour
yes_time = now_time + (hours=+0.5)
# Compare time sizes
if now_time>yes_time:
  print("ok")
else:
  print("no")
# Current time minus one day
# yes_time = now_time + (days=-1)
yes_time_nyr = yes_time.strftime('%Y-%m-%d %H:%M:%S')
print(yes_time_nyr)

Results:

import time, datetime
# A month ago
today1 = ()
astmonth = (, ( - 1), , , ,)

This python str string to uuid example above is all I have shared with you.