抽奖活动需要 需要在多个参与抽奖的消息列表里抽取一定的中奖用户,于是网上搜了一下写了一个简单的抽奖小代码
数据是user_list.xls格式的excel表格,抽奖的内容是填写的电子邮箱(内容在表格数据的最后一列中)
比如
| 姓名 | 性别 | 电子邮箱 |
| A | 男 | A@a.com |
| B | 女 | B@b.com |
excel表格内容如上图所示 (没有第一行说明信息)根据email进行抽奖
代码如下:
#!/usr/bin/env python
# coding=utf-8
import sys
import xlrd
import random
workbook = xlrd.open_workbook('user_list.xls')
excel_sheet = workbook.sheet_by_index(0)
nrows = excel_sheet.nrows
ncols = excel_sheet.ncols
users=[]
for i in range(0,nrows):
users.append(excel_sheet.row(i)[ncols-1].value)
result = random.sample(users,35)
print("中奖名单:")
for i in range(0,35):
print(result[i])
最后就会把中奖名单打印出来了。
