做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

在此生成由数字,字母组成的20位字符串

uuid模块

import uuid

def get_id():
file_object = open('uudi.txt','w+')
for i in range(200):
ID = str(uuid.uuid1()) + '\n'
file_object.write("ID"+str(i+1)+":"+ID)
file_object.close() if __name__ == '__main__':
get_id()

random模块

# coding:utf-8
# python3环境
# 通过多线程来生成200个激活码
import random
import string
import threading def get_str():
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 20))
return ran_str class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("开始线程:" + self.name)
write_id(self.name, 40)
print ("退出线程:" + self.name) def write_id(threadName, counter):
file_object=open('id.txt','a+')
while counter:
file_object.write("%s: %s\n" % (threadName+str(counter), get_str()))
counter -= 1
file_object.close() # 创建新线程
thread1 = myThread(1, "Group1-", 1 )
thread2 = myThread(2, "Group2-", 2)
thread3 = myThread(3, "Group3-", 3)
thread4 = myThread(4, "Group4-", 4)
thread5 = myThread(5, "Group5-", 5) # 开启新线程
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start() thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join() print ("退出主线程")

参考链接

https://docs.python.org/3/library/random.html

https://www.runoob.com/python/func-number-random.html

https://www.runoob.com/python/python-files-io.html

https://docs.python.org/3.6/library/uuid.html

最新文章

  1. iOS中assign,copy,retain之间的区别以及weak和strong的区别
  2. Struts框架2ActionError类 内部资料 请勿转载 谢谢合作
  3. HTML 表单和输入<textarea><label><fieldset><legend><select><optgroup><option><button>
  4. 要期末了搞不了OI了额……
  5. 常用jQuery代码02
  6. js将long日期格式转换为标准日期格式
  7. spring 学习的开源项目
  8. php 编译安装curl 时候出现问题
  9. 图片加 alt 属性
  10. 读取Execl表 导入数据库
  11. EasyUI DataGrid 基于 Ajax 自定义取值(loadData)
  12. 【Alpha】咸鱼冲刺日记第一天-黄紫仪
  13. Linux Debugging(七): 使用反汇编理解动态库函数调用方式GOT/PLT
  14. Leetcode_12_Integer to Roman
  15. 在CentOS上安装Python3的三种方法
  16. C++动态链接库方法调用
  17. array_column函数
  18. Oracle 强制中止正在执行的SQL语句
  19. C++ 初始化函数
  20. js 控件样式显隐和赋值

热门文章

  1. MariaDB 更新查询
  2. 编译php-5.5.15出错,xml2-config not found
  3. 【dart学习】-- Dart之async和await
  4. win10 解决telnet不是内部或外部命令的方案
  5. 设置当内容超出div(文字长度超出div宽度)出现滚动条
  6. postgresql的规则系统
  7. FFMpeg视频解码初探
  8. Tomcat_shutdown
  9. -webkit-box 高度自动填满
  10. 开启MySQL远程访问权限 允许远程连接(阿里云服务器)、linux关闭防火墙