code
#coding=utf-8 import os import random import time def test(book_type,book_id): #获取指定长度的随机数字 #支持16进制数的最大的长度为9 random_str_length=random.randint(3,6) indexlist=range(1,10) indexs=random.sample(indexlist,random_str_length) #随机字符串 random_str="" for i in indexs: random_str+=str(i) #假设book_id 长度<=9 book_id_length=len(str(book_id)) timestamp="".join(str(time.time()).split(".")) out_trade_no = timestamp+str(book_type)+random_str+str(random_str_length)+str(book_id)+str(book_id_length) return out_trade_no out_trade_no=test(80,1110) print(out_trade_no) #123123123165123310004 #解析出book_id,book_type book_id_length=int(out_trade_no[-1]) left=0-(book_id_length+1) book_id=int(out_trade_no[left:-1]) print(book_id) random_str_length=int(out_trade_no[0-(book_id_length+2)]) left=0-(random_str_length+book_id_length+4) right=0-(random_str_length+book_id_length+2) book_type=out_trade_no[left:right] print(book_type)