How to create a GUID/UUID in Python

前端 未结 8 1086
情话喂你
情话喂你 2020-11-22 16:47

How do I create a GUID in Python that is platform independent? I hear there is a method using ActivePython on Windows but it\'s Windows only because it uses COM. Is there a

8条回答
  •  春和景丽
    2020-11-22 17:41

    Check this post, helped me a lot. In short, the best option for me was:

    import random 
    import string 
    
    # defining function for random 
    # string id with parameter 
    def ran_gen(size, chars=string.ascii_uppercase + string.digits): 
        return ''.join(random.choice(chars) for x in range(size)) 
    
    # function call for random string 
    # generation with size 8 and string  
    print (ran_gen(8, "AEIOSUMA23")) 
    

    Because I needed just 4-6 random characters instead of bulky GUID.

提交回复
热议问题