2013-04-07 21 views
26

Benpython: Geçerli bir uuid, String'den UUID'ye nasıl dönüştürülür?

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

olarak veri almak ve python docs üzerinde bir yolunu bulamadık ben uuid

için String den uuid dönüştürmek gerekir, ya da ben burada temel bir şey eksik?

+0

Sen kontrol etmek isteyebilirsiniz [bu diğer soru/cevap] (http://stackoverflow.com/questions/534839/how-to-create-a-guid-in-python?rq=1) ve ayrıca [burada dokümanlar] (http://docs.python.org/2/library/uuid.html). :) – summea

cevap

41

Sadece uuid.UUID iletecek:

import uuid 

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

print uuid.UUID(o['uuid']).hex 
0

yukarıdaki cevabı uuid.UUID(your_uuid_string) benim için çalıştı kullanarak ... gerçek bir UUID nesneye geri dize biçiminde geçerli bir UUID dönüştürmek için sizin için işe yaramadıysa.

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'> 
İlgili konular