随笔
import xlwt import time from random import * import numpy as np title = ['交易时间','用户ID','订单号','应用名称' ,'金额','状态'] import random def generate_rand(n, sum_v): Vector = [random.random() for i in range(n)] Vector = [ int(i / sum(Vector) * sum_v) for i in Vector] if sum(Vector) < sum_v: Vector[0] += sum_v-sum(Vector) return Vector y=generate_rand(2476,57480) #print(len(y)) def gener(n,a1,a2): start=time.mktime(a1) #生成开始时间戳 end=time.mktime(a2) #生成结束时间戳it #随机生成10个日期字符串 date_1=[] for i in range(n): t=random.randint(start,end) #在开始和结束时间戳中随机取出一个 date_touple=time.localtime(t) #将时间戳生成时间元组 date=time.strftime("%Y-%m-%d %H:%M:%S",date_touple) #将时间元组转成格式化字符串(1976-05-21) date_1.append(date) return date_1 k=gener(2476,(2018,5,1,0,0,0,0,0,0),(2018,5,1,23,59,59,0,0,0)) print(k) stus=[] for i in range(2476): stus.append([k[i],'用户ID','订单号','应用名称' ,y[i],'成功']) print(stus) #stus = [['mary',20,'女',89.9],['mary',20,'女',89.9],['mary',20,'女',89.9],['mary',20,'女',89.9]] #新建一个excel对象 wbk = xlwt.Workbook() #添加一个名为 课程表的sheet页 sheet = wbk.add_sheet('stu') for i in range(len(title)):#写入表头 sheet.write(0,i,title[i])#写入每行,第一个值是行,第二个值是列,第三个是写入的值 for i in range(len(stus)): if i !=0:#如果不是表头的话 for j in range(6): sheet.write(i,j,stus[i][j])#循环写入每行数据 #保存数据到‘test.xls’文件中 wbk.save('szz2.xls')#保存excel必须使用后缀名是.xls的,不是能是.xlsx的