抽獎活動需要 需要在多個參與抽獎的消息列表里抽取一定的中獎用戶,於是網上搜了一下寫了一個簡單的抽獎小代碼
數據是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])
最后就會把中獎名單打印出來了。
