Python實現excel表不重復隨機抽樣


抽獎活動需要 需要在多個參與抽獎的消息列表里抽取一定的中獎用戶,於是網上搜了一下寫了一個簡單的抽獎小代碼

數據是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])

  最后就會把中獎名單打印出來了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM