How to create a GUID/UUID in Python

前端 未结 8 1087
情话喂你
情话喂你 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:22

    This function is fully configurable and generates unique uid based on the format specified

    eg:- [8, 4, 4, 4, 12] , this is the format mentioned and it will generate the following uuid

    LxoYNyXe-7hbQ-caJt-DSdU-PDAht56cMEWi

     import random as r
    
     def generate_uuid():
            random_string = ''
            random_str_seq = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
            uuid_format = [8, 4, 4, 4, 12]
            for n in uuid_format:
                for i in range(0,n):
                    random_string += str(random_str_seq[r.randint(0, len(random_str_seq) - 1)])
                if n != 12:
                    random_string += '-'
            return random_string
    

提交回复
热议问题