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

参考网上的文章来学习,感谢原作者!(不好意思,忘了在哪看的了)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2016/12/12 下午1:10
# @Author  : hukezhu
# @Site    : 
# @File    : 0001:生成200个激活码.py
# @Software: PyCharm


#这是使用随机数的方法

# import string as s
# import random as r
#
#
# def randomNum():
#     f = open('/Users/hukezhu/Desktop/随机数激活码.txt','w+')
#     for i in range(200):
#         randomStr = ''.join(r.choice(s.ascii_uppercase + s.digits) for x in  range(10)) + '\n'
#         f.write(randomStr)
#     f.close()
#
#
# if __name__ == '__main__':
#     randomNum()



#直接使用UUID模块

# import uuid as u
#
# def rand_uuid():
#     f = open('/Users/hukezhu/Desktop/UUID随机激活码.txt','w+')
#     for i in range(200):
#         ID = str(u.uuid1()) + '\n'
#         f.write(ID)
#
#     f.close()
#
#
#
# if __name__ == '__main__':
#     rand_uuid()





#使用主键和随机码
import string as  s
import random as  r

def rand_ID():
    f = open('/Users/hukezhu/Desktop/主键和随机码随机码.txt','w+')

    for i in range(200):
        ID = id(i)
        rand_id = str(ID)[5:]
        null = ''
        for j in rand_id:
            null += j + r.choice(s.ascii_uppercase)

        f.write(null + '\n')
    f.close()


if __name__ == '__main__':
    rand_ID()




    原文作者:hu9134
    原文地址: https://www.jianshu.com/p/74c5dc3e0e3d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞