python: how to convert a valid uuid from String to UUID?
Asked Answered
E

5

130

I receive the data as

   {
        "name": "Unknown",
        "parent": "Uncategorized",
        "uuid": "06335e84-2872-4914-8c5d-3ed07d2a2f16"
    },

and I need to convert the uuid from String to uuid

I did not find a way on the python docs, or am I missing something basic here?

Extensile answered 7/4, 2013 at 5:21 Comment(1)
You may want to check out this other question/answer and also the docs here. :)Orontes
U
158

Just pass it to uuid.UUID:

import uuid

o = {
    "name": "Unknown",
    "parent": "Uncategorized",
    "uuid": "06335e84-2872-4914-8c5d-3ed07d2a2f16"
}

print uuid.UUID(o['uuid']).hex
Unwashed answered 7/4, 2013 at 5:24 Comment(0)
V
38

Don't call .hex on the UUID object unless you need the string representation of that uuid.

>>> import uuid
>>> some_uuid = uuid.uuid4()
>>> type(some_uuid)
<class 'uuid.UUID'>
>>> some_uuid_str = some_uuid.hex
>>> some_uuid_str
'5b77bdbade7b4fcb838f8111b68e18ae'
>>> type(some_uuid_str)
<class 'str'>

Then as others mentioned above to convert a uuid string back to UUID instance do:

>>> uuid.UUID(some_uuid_str)
UUID('5b77bdba-de7b-4fcb-838f-8111b68e18ae')
>>> (some_uuid == uuid.UUID(some_uuid_str))
True
>>> (some_uuid == some_uuid_str)
False

You could even set up a small helper utility function to validate the str and return the UUID back if you wanted to:

def is_valid_uuid(val):
    try:
        return uuid.UUID(str(val))
    except ValueError:
        return None

Then to use it:

>>> some_uuid = uuid.uuid4()
>>> is_valid_uuid(some_uuid)
UUID('aa6635e1-e394-463b-b43d-69eb4c3a8570')
>>> type(is_valid_uuid(some_uuid))
<class 'uuid.UUID'>
Vasti answered 22/2, 2019 at 14:43 Comment(0)
E
11

If the above answer didn't work for you for converting a valid UUID in string format back to an actual UUID object... using uuid.UUID(your_uuid_string) worked for me.

In [6]: import uuid
   ...:
   ...: o = {
   ...:     "name": "Unknown",
   ...:     "parent": "Uncategorized",
   ...:     "uuid": "06335e84-2872-4914-8c5d-3ed07d2a2f16"
   ...: }
   ...:
   ...: print uuid.UUID(o['uuid']).hex
   ...: print type(uuid.UUID(o['uuid']).hex)
06335e84287249148c5d3ed07d2a2f16
<type 'str'>

In [7]: your_uuid_string = uuid.UUID(o['uuid']).hex

In [8]: print uuid.UUID(your_uuid_string)
06335e84-2872-4914-8c5d-3ed07d2a2f16

In [9]: print type(uuid.UUID(your_uuid_string))
<class 'uuid.UUID'>
Entertaining answered 27/6, 2017 at 21:21 Comment(0)
G
2

Based on @slajma's answer here is a utility function that returns True/False:

def is_valid_uuid(val):
try:
    uuid.UUID(str(val))
    return True
except ValueError:
    return False
Gasaway answered 15/6, 2023 at 7:55 Comment(0)
I
0

I don't get it, it simply does not work for me..

My code:

import uuid


def is_valid_uuid(uuid_to_test, version=4):
    """
    Check if uuid_to_test is a valid UUID.
    """
    try:
        uuid_obj = uuid.UUID(uuid_to_test.strip(), version=version)
    except ValueError:
        return False

    # return comparison
    return str(uuid_obj) == uuid_to_test.strip()


# uuid_to_test = '7de8d929-5f13-e911-9110-0050569e73eb'

when the comparison is done, it seems like the str(uuid_obj) becomes: '7de8d929-5f13-4911-9110-0050569e73eb'
instead the expected:
'7de8d929-5f13-e911-9110-0050569e73eb'

Indigestion answered 18/4 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.