在Python中生成GUID(全局唯一标识符)非常简单,你可以使用内置的`uuid`模块。以下是一个示例代码,展示了如何生成一个随机的GUID:
import uuidunique_id = uuid.uuid4()print(unique_id)
运行上述代码将会输出一个形如`4f3c66af-6d23-4e6d-9a11-e1f9e4`的GUID。
如果你需要更多的选项,比如生成大写或小写的GUID,或者查看版本信息,你可以使用以下脚本:
import uuidimport sysdef show_ver():print('guid generator v1.0, by hydonlee')def show_usage():show_ver()print("""generate a new guid and print it in stdout.Usage: newguid [option]options:-v get the version information-h show the help information-u,-U show guid in uppercase, default is uppercase-l,-L show guid in lowercase""")if __name__ == '__main__':isUpper = Trueact = 'uuid'if '-u' in sys.argv:isUpper = Trueif '-U' in sys.argv:isUpper = Trueif '-l' in sys.argv:isUpper = Falseif '-L' in sys.argv:isUpper = Falseif '-v' in sys.argv:act = 'ver'if '-h' in sys.argv:act = 'help'if act == 'uuid':unique_id = uuid.uuid4()if isUpper:print(unique_id.upper())else:print(unique_id.lower())elif act == 'ver':show_ver()elif act == 'help':show_usage()
这个脚本允许你通过命令行参数来控制输出的GUID的大小写和查看版本信息

