2

I am using python v3.6, in Odoo framework.

I wanted to generate uuid, so I am using uuid as follows:

:~$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uuid
>>> id =  uuid.uuid1().hex
>>> id
'daf59b684d6a11xz9a9c34028611c679'
>>> 

How can I get uuid? separated by hyphens and dividing string into four parts as : daf59b684-d6a11x-z9a9c3402-8611c679

I tried finding it on SO, but didn't get, instead got a reverse-one, opposite of my requirements as link.

how to create hyphens in betweens uuid-string, like this??

UUID-Docs link.

SiHa
  • 7,830
  • 13
  • 34
  • 43
GD- Ganesh Deshmukh
  • 1,456
  • 3
  • 24
  • 36
  • Related: [How can I format a string to UUID format that contains hyphens](https://stackoverflow.com/q/55420773/7851470) – Georgy Dec 23 '20 at 09:18

2 Answers2

11

You just want to convert the UUID into a string:

>>> import uuid
>>> print(uuid.uuid1())
74ba3af4-4d6d-11ea-989f-784f435149ee
>>> u = str(uuid.uuid1())
>>> u
'7d7b626c-4d6d-11ea-989f-784f435149ee'

Note that this is clearly documented:

str(uuid) returns a string in the form 12345678-1234-5678-1234-567812345678 where the 32 hexadecimal digits represent the UUID.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

I'm afraid you have a misconception about UUIDs. Those are just numbers, 128 binary digits is their inherent representation. The hex property just provides those as string formatted as hexadecimal. If you want the dashes in there, you can add them yourself easily, since you have the guarantee that you have 32 hexadecimal digits.

However, just read help(uuid), because there's an even easier way to get what you want. Just convert the object to a string using str(id).

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55