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

前端 未结 3 694
花落未央
花落未央 2021-02-03 16:47

I receive the data as

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


        
3条回答
  •  情话喂你
    2021-02-03 17:41

    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
    
    
    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))
    
    

提交回复
热议问题